How to Subset a Dataset When Plotting with GGPLOT2



How to Subset a Dataset When Plotting with GGPLOT2

This article describes how to subset data when creating a ggplot.

Related Book

GGPlot2 Essentials for Great Data Visualization in R

Prerequisites

Load required packages and set the theme function theme_bw() as the default theme:

library(ggplot2) 
theme_set(theme_bw())

Data preparation

Data derived from ToothGrowth data sets are used. ToothGrowth describes the effect of Vitamin C on tooth growth in Guinea pigs. Three dose levels of Vitamin C (0.5, 1, and 2 mg) with each of two delivery methods [orange juice (OJ) or ascorbic acid (VC)] are used :

df <- data.frame(
  supp = rep(c("VC", "OJ"), each = 3),
  dose = rep(c("D0.5", "D1", "D2"), 2),
  len = c(6.8, 15, 33, 4.2, 10, 29.5)
  )

head(df)
##   supp dose  len
## 1   VC D0.5  6.8
## 2   VC   D1 15.0
## 3   VC   D2 33.0
## 4   OJ D0.5  4.2
## 5   OJ   D1 10.0
## 6   OJ   D2 29.5
  • len : Tooth length
  • dose : Dose in milligrams (0.5, 1, 2)
  • supp : Supplement type (VC or OJ)

Create a plot the whole dataset

ggplot(df, aes(x = dose, y = len))+
  geom_col(aes(fill = supp), width = 0.7) +
  scale_fill_viridis_d()

Subset the dataset

ggplot(subset(df, dose %in% c("D0.5", "D1")), aes(x = dose, y = len))+
  geom_col(aes(fill = supp), width = 0.7) +
  scale_fill_viridis_d()

Version: Français





No Comments

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments