1
votes

I am trying to display grouped boxplot and combined boxplot into one plot. Take the iris data for instance:

data(iris)
p1 <- ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
  geom_boxplot()
p1

boxplot

I am trying to compare overall distribution with distributions within each categories. So is there a way to display a boxplot of all samples on the left of these three grouped boxplots?

Thanks in advance.

2

2 Answers

3
votes

You can rbind a new version of iris, where Species equals "All" for all rows, to iris before piping to ggplot

p1 <- iris %>% 
        rbind(iris %>% mutate(Species = 'All')) %>% 
        ggplot(aes(x = Species, y = Sepal.Length)) + 
          geom_boxplot()
3
votes

Yes, you can just create a column for all species as follows:

iris = iris %>% mutate(all = "All Species")
p1 <- ggplot(iris) + 
  geom_boxplot(aes(x=Species, y=Sepal.Length)) +
  geom_boxplot(aes(x=all, y=Sepal.Length))
p1

output