Home > OS >  ggplot2 - Error while generating a horizontal boxplot with custom whiskers using geom_boxplot
ggplot2 - Error while generating a horizontal boxplot with custom whiskers using geom_boxplot

Time:04-06

For educational purposes, I am trying to generate a horizontal boxplot combined with a dotplot using ggplot2. However, I would like to generate a custom boxplot with whiskers at the 2.5% and 97.5% percentiles instead of the 1.5*IQR definition that is used by geom_boxplot. Therefore, I decided to use the following code:

y <- rnorm(100)
df = data.frame(y)
df_boxplot <- data.frame(
  x_coord = 0.5,
  y0 = quantile(y, 0.025),
  y25 = quantile(y, 0.25),
  y50 = median(y),
  y75 = quantile(y, 0.75),
  y100 = quantile(y, 0.975)
)
# Vertical orientation with custom whiskers works
ggplot()  
  geom_boxplot(data = df_boxplot,
    aes(x = x_coord, ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
    stat = "identity"
  )  
  geom_jitter(data = df, aes(y=y, x=0.5))
# Horizontal orientation with custom whiskers throws an error
ggplot()  
  geom_boxplot(data = df_boxplot,
               aes(y=x_coord, xmin = y0, xlower = y25, xmiddle = y50, xupper = y75, xmax = y100),
               stat = "identity"
  )  
  geom_dotplot(data = df, aes(x=y))

# Using horizontal stat_summary with custom whiskers works
f <- function(x) {
  r <- quantile(x, probs = c(0.025, 0.25, 0.5, 0.75, 0.975))
  names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
  r
}
ggplot()  
  stat_summary(data = df, aes(x=y, y=0.5), fun.data=f, geom="boxplot", position="dodge2", orientation = "y")  
  geom_dotplot(data = df, aes(x=y))

However, when using the horizontal orientation together with the custom definition of whiskers, I get the following error:

Error in `$<-.data.frame`(`tmp`, "xmin", value = numeric(0)) :

Replacement has 0 rows, data has 1

This error seems to be geom_boxplot specific, as it works fine with stat_summary. I would like to ask what the reason for the error in geom_boxplot is and how to fix it. The plot is supposed to look something like this:

Combined dotplot and boxplot

CodePudding user response:

You need to set orientation = 'y' to get the horizontal boxplot directly from geom_boxplot. You might think geom_boxplot would guess this from the supplied aesthetics, but it doesn't.

ggplot()  
  geom_boxplot(data = df_boxplot,
               aes(y = x_coord, xmin = y0, xlower = y25, xmiddle = y50, 
                   xupper = y75, xmax = y100),
               stat = "identity", orientation = 'y'
  )  
  geom_dotplot(data = df, aes(x=y))

enter image description here

  • Related