adapting jitter position to match that of dodged boxplots

16 views
Skip to first unread message

Matthieu Dubois

unread,
Jun 13, 2009, 6:00:45 AM6/13/09
to Ggplot2 Help
Hi everybody, 

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. 

##CODE: 
# 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
## END of the CODE

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. 

Any help would be appreciated. 

Regards, 

Matthieu

Matthieu Dubois
Post-doctoral fellow

Psychology and NeuroCognition Lab (CNRS UMR 5105)
Université Pierre Mendès-France
BP47 --- 38040 Grenoble Cedex 9 --- France





hadley wickham

unread,
Jun 15, 2009, 9:44:43 AM6/15/09
to Matthieu Dubois, Ggplot2 Help
Hi Matthieu,

Unfortunately this is basically impossible, because of the way that
position adjustments work - they work with individual records (after
statistical transformation). For the boxplots, there is a row for
each group, while for the points there is a row for each point. You
can sometimes do want you want using facetting instead of dodging.

Regards,

Hadley
--
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages