Let's borrow the reproducible example provided by Kevin Ushey in this question:
set.seed(123)
dat <- data.frame(
x=rep( c(1, 2, 3, 4), times=25 ),
y=rnorm(100),
gp=rep(1:2, each=50)
)
p <- ggplot(dat, aes(x=factor(x), y=y))
p + geom_boxplot(aes(fill = factor(gp))) #example 1
Then, following the advice of Arun, I tested the (position = position_dodge(.))
but with geom_boxplot
instead of geom_bar
, and it worked.
In this case, it was not necessary to change the boxes width.
So, changing the last line of the above code to:
p + geom_boxplot(aes(fill = factor(gp)),position=position_dodge(1))
did the trick.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…