How to put overall distribution boxplot along side grouped boxplots in R
To make multiple boxplots of column "xcolumn" based on a group, I can simply do:
boxplot(xcolumn ~ group, data = df)
And to plot the overall distribution:
boxplot(df$xcolumn)
But, is it possible to have the overall distribution of xcolumn in the same plot with the grouped boxplot? I want to have the overall distribution as the first boxplot, followed by boxplots from each group.
2 answers

You can add a new column for the overall group, then use
ggplot
:library(dplyr) library(ggplot2) iris %>% mutate(Group = "all") %>% ggplot() + geom_boxplot(aes(Species, Sepal.Length)) + geom_boxplot(aes(Group, Sepal.Length))

You just duplicate the values and give them a new name, say "tot", and
rbind()
it together. Userelevel()
to move the new factor level to the front.set.seed(1) dtf < data.frame(g=rep(c("A", "B"), 12), a=rnorm(24)+(2:1)) tot < dtf tot$g < "tot" dtf.tot < rbind(dtf, tot) dtf.tot$g < relevel(dtf.tot$g, "tot") boxplot(a ~ g, data=dtf.tot)