Say you have the following dataset:
trt <- ifelse(runif(100)<0.5,"drug","placebo")
inj.site <- ifelse(runif(100)<0.5,"ankle","wrist")
relief <- 20 + 0.5*(inj.site=="ankle") + 0.5*(trt=="drug") + rnorm(100)
to.analyze <- data.frame(trt,inj.site,relief)
Now, the idea is to make a boxplot with injury site on the x-axis and boxes by treatment side-by-side:
bplot <- ggplot(to.analyze,aes(inj.site,relief,fill=trt)) + geom_boxplot(position="dodge")
Easy enough. But now I want to add raw data points on top of the boxes. If I didn't have boxes with position="dodge"
, this would be easy:
bplot + geom_point(aes(colour=trt))
However, this draws points in between the boxes, and adding a position="dodge"
to this geometry does not seem to work. How do I adjust this so that points are drawn over the boxes?
Bonus: same situation with using stat_summary(blah,y.fun=mean,shape="+")
to overplot the means, which has the same issue.
Hadley will doubtless correct me if I'm wrong here...
Here's the natural syntax:
bplot + geom_point(aes(colour=trt), position=position_dodge(width=.5))
(position="dodge" will do the same thing, without the parameter.)
When I plot it, I get something that looks like a position_jitter(), which is presumably what you get too.
Curious, I went to look in the source, where I found the pos_dodge() function. (Type pos_dodge at an R prompt to see it...) Here's the end of it:
within(df, {
xmin <- xmin + width / n * (seq_len(n) - 1) - diff * (n - 1) / (2 * n)
xmax <- xmin + d_width / n
x <- (xmin + xmax) / 2
})
n is the number of rows of the data frame. So it looks like it's dodging the individual points by a fraction indexed by the row! So the first point is dodged width/n, the second is dodged 2 * width/n, and the last is dodged n * width/n.
This is obviously not what you meant, although it is what you said. You may be stuck recreating the dodged boxplot manually, or using a different visualization, like faceting maybe?
ggplot(to.analyze,aes(inj.site,relief)) + geom_boxplot() + facet_wrap(~ trt)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With