Getting an error that ggplot2_3.2.0 can't draw more than one boxplot per group
Asked Answered
L

2

6

I have xy data that I'd like to plot using R's ggplot:

library(dplyr)
library(ggplot2)
set.seed(1)

df <- data.frame(group = unlist(lapply(LETTERS[1:5],function(l) rep(l,5))),
                 x = rep(1:5,5),
                 y = rnorm(25,2,1),
                 y.se = runif(25,0,0.1)) %>%
  dplyr::mutate(y.min = y-3*y.se,
                y.low = y-y.se,
                y.high = y+y.se,
                y.max = y+3*y.se)

As you can see, while df$x is a point (integer), df$y has an associated error, which I would like to include using a box plot.

So my purpose is to plot each row in df by its x coordinate, using y.min, y.low, y, y.high, and y.max to construct a boxplot, and color and fill it by group. That means, that I'd like to have each row in df, plotted as a box along a separate x-axis location and faceted by df$group, such that df$group A's five replicates appear first, then to their right df$group B's replicates, and so on.

This code used to work for my purpose until I just installed the latest ggplot2 package (ggplot2_3.2.0):

ggplot(df,aes(x=x,ymin=y.min,lower=y.low,middle=y,upper=y.high,ymax=y.max,col=group,fill=group))+
geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+
facet_grid(~group,scales="free_x")+scale_x_continuous(breaks = integerBreaks())

Now I'm getting this error:

Error: Can't draw more than one boxplot per group. Did you forget aes(group = ...)?

Any idea?

Landsman answered 25/7, 2019 at 0:14 Comment(3)
I'm getting the same new error, and my boxplot does not have groups or facets.Scopp
Looks like they patched something and now the group aesthetic is required. github.com/tidyverse/ggplot2/issues/3316Scopp
cran.r-project.org/web/packages/ggplot2/news/news.html (see 3.2.0)Scopp
M
4

You need a separate boxplot for each combination of x and group, so you can set the group aesthetic to interaction(x, group):

ggplot(df,aes(x=x,ymin=y.min,lower=y.low,middle=y,upper=y.high,
              ymax=y.max,col=group,fill=group))+
    geom_boxplot(aes(group = interaction(x, group)), 
                 position=position_dodge(width=0),
                 alpha=0.5,stat="identity")
Mesocratic answered 25/7, 2019 at 1:57 Comment(3)
Thanks @Marius, but this makes the boxes of each group appear on the same x-axis location. My intention was that each box has a separate x-axis location. I've edited my question accordingly.Landsman
The boxes are appearing at the same x-axis location because you have width = 0 in position_dodge, I assumed that was intentional. With width = 0.9 they will be spread out.Mesocratic
Thanks!. How about changing the order on the x-axis such that it is first ordered by df$group and then by df$x?Landsman
M
4

This code used to work for my purpose until I just installed the latest ggplot2 package (ggplot2_3.2.0)

You are right: I just experienced a similar error with a code I recently wrote using ggplot2 boxplots, and just find out this new error related to ggplot2 latest update. As Marius already pointed out, specifying the group in the aes() did solve the problem for me also. However I don't understand the rest of his answer, as it is not providing the faceting...

Here is a working solution with facet_grid(), you were close:

ggplot(df,aes(x=x,ymin=y.min,lower=y.low,middle=y,upper=y.high,ymax=y.max,col=group,fill=group, group=x))+
geom_boxplot(position=position_dodge(width=0),alpha=0.5,stat="identity")+
facet_grid(~group,scales="free_x")

enter image description here

Mycorrhiza answered 27/8, 2019 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.