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:


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

  • answered 2019-06-11 23:14 neilfws

    You can add a new column for the overall group, then use ggplot:

    iris %>% 
      mutate(Group = "all") %>% 
      ggplot() + 
      geom_boxplot(aes(Species, Sepal.Length)) + 
      geom_boxplot(aes(Group, Sepal.Length))

    enter image description here

  • answered 2019-06-11 23:32 AkselA

    You just duplicate the values and give them a new name, say "tot", and rbind() it together. Use relevel() to move the new factor level to the front.

    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)

    enter image description here