ggplot2 - jitter and position dodge together

Solution 1:

In ggplot2 version 1.0.0 there is new position named position_jitterdodge() that is made for such situation. This postion should be used inside the geom_point() and there should be fill= used inside the aes() to show by which variable to dodge your data. To control the width of dodging argument dodge.width= should be used.

ggplot(ex5, aes(x=tailindex, y=hillest, color=memorypar, fill=memorypar)) +
      facet_wrap(~process, nrow=2) +
      geom_point(position=position_jitterdodge(dodge.width=0.9)) +
      geom_boxplot(fill="white", outlier.colour=NA, position=position_dodge(width=0.9))

enter image description here

Solution 2:

EDIT: There is a better solution with ggplot2 version 1.0.0 using position_jitterdodge. See @Didzis Elferts' answer. Note that dodge.width controls the width of the dodging and jitter.width controls the width of the jittering.

I'm not sure how the code produced the graph in the pdf.

But does something like this get you close to what you're after?

I convert tailindex and memorypar to numeric; add them together; and the result is the x coordinate for the geom_jitter layer. There's probably a more effective way to do it. Also, I'd like to see how dodging geom_boxplot and geom_jitter, and with no jittering, will produce the graph in the pdf.

library(ggplot2)
dodge <- position_dodge(width = 0.9)
ex5$memorypar2 <- as.numeric(ex5$tailindex) + 
  3 * (as.numeric(as.character(ex5$memorypar)) - 0.2) 

p <- ggplot(ex5,aes(x=tailindex , y=hillest)) +
   scale_x_discrete() +
   geom_jitter(aes(colour = memorypar, x = memorypar2), 
     position = position_jitter(width = .05), alpha = 0.5) +
   geom_boxplot(aes(colour = memorypar), outlier.colour = NA, position = dodge) +
   facet_wrap(~ process, nrow = 2)
p

enter image description here