Skip to content

Default width inconsistent between geom_boxplot and others #2404

@idemockle

Description

@idemockle

The default width of geom_boxplot seems to be 0.75*(resolution), whereas for stat_identity it is 0.9*(resolution). This causes unexpected behavior when using stat = "identity" in geom_boxplot, because the default width is changed. The default width of boxplots seems to be undocumented, or at least I could not find it anywhere.

This took me an annoyingly long time to figure out, and I think it'd be great if these values could be made consistent to aid in user-friendliness. Barring that, documenting the width calculation in the doc for geom_boxplot would also be very helpful.

library(tidyverse)

# Example data
ncat1 <- 2
ncat2 <- 3
a <- data.frame(cat1 = factor(rep(1:ncat1, each = ncat2*100)), 
                cat2 = factor(rep(rep(1:ncat2, each = 100), ncat1)), 
                val = rnorm(ncat1*ncat2*100))
abox <- a %>% 
  group_by(cat1, cat2) %>% 
  summarise(ymin = min(val),
            lower = quantile(val, .25),
            middle = median(val),
            upper = quantile(val, .75),
            ymax = max(val),
            mean = mean(val))

# Plot boxplots and means with default boxplot stat and dodge width = 0.75
a %>% 
  ggplot(aes(cat1, val)) + 
    geom_boxplot(aes(fill = cat2)) + 
    geom_point(data = abox, aes(group = cat2, y = mean), 
               position = position_dodge(width = .75),
               color = 'blue', 
             size = 3)

# Plot boxplots and means with identity stat, having whiskers cover entire range of data, 
# and with dodge width = 0.75 (points misaligned)
abox %>% 
  ggplot(aes(cat1)) + 
    geom_boxplot(aes(fill = cat2, 
                     ymin = ymin, 
                     lower = lower,
                     middle = middle,
                     upper = upper,
                     ymax = ymax),
                 stat = 'identity') + 
    geom_point(aes(group = cat2, y = mean), 
               position = position_dodge(width = .75),
               color = 'blue', 
               size = 3)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions