position_dodge2() works without a grouping variable in a layer
When working with position_dodge(), every layer that is being dodged needs to have the variable that represents the groups present. When working with multiple layers that need dodging this usually means mapping the variable globally or making sure each layer as an aesthetic mapping involving that grouping variable.
This is not true for position_dodge2(), which seems to allow dodging for all layers as long as any layer has the variable mapped.
Here is an example to that shows position_dodge2() working where position_dodge() fails. Is this the expected behavior?
library(ggplot2)
dat = data.frame(bin = factor(c(1,2,3,1,2,3)),
type = factor(c(1,1,1,2,2,2)),
beta = c(10,20,30,40,50,60))
# Put "fill" in global aes() and dodging works in all layers
ggplot(dat, aes(x = bin, y = beta, fill = type)) +
geom_col(position = "dodge") +
geom_errorbar(aes( ymin = beta - 4, ymax = beta + 4),
position = position_dodge(width = .9) )

# Move "fill" to geom_bar() and geom_errorbar() doesn't dodge
ggplot(dat, aes(x = bin, y = beta)) +
geom_col(aes(fill = type), position = "dodge") +
geom_errorbar(aes( ymin = beta - 4, ymax = beta + 4),
position = position_dodge(width = .9) )

# But if use position_dodge2() all layers dodge
ggplot(dat, aes(x = bin, y = beta)) +
geom_col(aes(fill = type), position = "dodge2") +
geom_errorbar(aes( ymin = beta - 4, ymax = beta + 4),
position = position_dodge2(width = .9) )

Created on 2018-07-20 by the reprex package (v0.2.0).
The position_*() functions dodge based on the x/xmin/xmax of items on the panel. position_dodge() and position_dodge2() have different strategies, and as I understand it position_dodge() incorporates the group whereas position_dodge2() does not. Therefore, I think that the behaviour you describe is entirely correct!
Oh, good to know!
What do you think about clarifying the documentation to indicate the two functions work differently with grouping variables? I see questions that boil down to "why isn't my xxx layer dodging" due to a missing global or layer specific grouping variable and position_dodge() regularly on Stack Overflow. This different behavior between the two dodging functions and grouping variables could confuse that issue a little more. :slightly_smiling_face:
I can get on board with that!
Hi,
In addition to your examples, i tried adding fill argument locally to aes() both in the geom_col() and geom_errorbar(). While the code threw a warning such as "Warning message:
Ignoring unknown aesthetics: fill" , it produced what is desired. I wonder how this is possible. I would appreciate any suggestion. Thank you very much.
library(ggplot2)
dat = data.frame(bin = factor(c(1,2,3,1,2,3)),
type = factor(c(1,1,1,2,2,2)),
beta = c(10,20,30,40,50,60))
ggplot(dat, aes(x = bin, y = beta)) +
geom_col(aes(fill = type), position = "dodge") +
geom_errorbar(aes(fill = type, ymin = beta - 4, ymax = beta + 4), #<<
position = position_dodge(width = .9) )
Warning message: Ignoring unknown aesthetics: fill
