Home > Enterprise >  Why does `color` override `position` in ggplot?
Why does `color` override `position` in ggplot?

Time:10-11

I'm having a problem whereby a setup that works for position-dodging gets disabled if I specify an alternative color. Here is a reproducible example:

library(emmeans)
noise.lm = lm(noise ~ size * type * side, data = auto.noise)

emmip(noise.lm, side * size ~ type, CIs = TRUE,
      CIarg = list(lwd = 1, alpha = 0.5, linetype = "solid"))

This produces the following plot plot with automatic colors

Note that the confidence intervals (vertical bars) are offset from one another. If, however, I do exactly the same thing but add a color specification:

emmip(noise.lm, side * size ~ type, CIs = TRUE,
      CIarg = list(lwd = 1, alpha = 0.5, linetype = "solid", color = "gray"))

Then I get this result

plot with specified colors

... and the offsetting seems to have been disabled. Why is that and how do I avoid it?

More details: the CIarg argument in emmip() is used to specify additional arguments to the code that produces the confidence intervals. The relevant code within emmeans::emmip() is

    if (CIs) {
        CIarg$mapping = ggplot2::aes_(ymin = ~LCL, ymax = ~UCL)
        CIarg$position = pos
        grobj = grobj   do.call(ggplot2::geom_linerange, CIarg)
    }

where grobj is the plot object being created, and (earlier in the code) pos was created via

    pos = ggplot2::position_dodge(width = ifelse(CIs|PIs, dodge, 0)) # use dodging if CIs

where dodge = 0.1 (default argument value).

CodePudding user response:

The issue is that the grouping and hence the dodging is determined by the color aesthetic. Using color="grey" you override the grouping and the lineranges for the CIs no longer get dodged. I don't see any option to avoid that, only one to fix that by fixing the mapping, i.e. adding the group aes to the geom_linerange like so:

Note: tvar is the variable mapped on the color aes.

library(emmeans)
noise.lm = lm(noise ~ size * type * side, data = auto.noise)

p <- emmip(noise.lm, side * size ~ type, CIs = TRUE,
      CIarg = list(lwd = 1, alpha = 0.5, linetype = "solid", color = "grey"))

p$layers[[3]]$mapping <- ggplot2::aes_(ymin = ~LCL, ymax = ~UCL, group = ~tvar)

p

  • Related