Can we make a boxplot for only 2 categories in the column

I have a sample dataframe below

DF <- data.frame(site = rep(LETTERS[1:7], each=20), y = runif(7*20))

This Dataframe has 7 Letters(Categories). Is there a way to make boxplot for only A and B (for any 2 categories) categories side by side without transforming the dataset. I trying with the below code but not sucessful

ggplot(DF)+geom_boxplot(mapping = aes(site~A+B,y=y)).

Please guide

You can filter the dataset before sending it to the ggplot, or filter the data just when you load this into the ggplot-call.

#option 1
filter(DF, site == "A" | site == "B") %>%
  ggplot() + geom_boxplot(aes(x = site, y = y))

# option 2
ggplot(data = filter(DF, site == "A" | site == "B")) + 
  geom_boxplot(aes(x = site, y = y))

Perfect thanks a lot

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.