I'm pursuing my learning of ggplot2. I now would like to do the following graph: jittered points superimposed over box plots, that were dodged according to a third grouping factor. The following code do most of the job, except that I would like to restrict the x positions of the jittered points to match the 'x' extend of the box plot corresponding to their group. On the current plot, the 'x' extend of jittered dots overlap the 'x' extend of the different groups' boxplots. My explanation of the problem may seem obscure, but I hope the code will render it more understandable.
# generate some data
# composed of two groups (group: control vs patient), five tasks (x: tasks 1 to 5) and participants' response (y)
data <- expand.grid(group=rep(c('control', 'patient'), c(20,10)),
x= factor(paste('task', 1:5, sep="")))
data$y <- rnorm(data$x, mean=ifelse(data$group=='control', 10, 5), sd=ifelse(data$group=='control', 1, 2))
# plot
p <- ggplot(data, aes(x=x, y=y, col=group)) +
geom_boxplot(outlier.size=0) +
geom_jitter()
p
To make it clearer, I would like to superimpose the red points only over the red boxplots, and the green points over the green boxplots, separately for each task.