Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 - jitter and position dodge together

Tags:

r

ggplot2

I am trying to recreate a figure from a GGplot2 seminar http://dl.dropbox.com/u/42707925/ggplot2/ggplot2slides.pdf.

In this case, I am trying to generate Example 5, with jittered data points subject to a dodge. When I run the code, the points are centered around the correct line, but have no jitter.

Here is the code directly from the presentation.

set.seed(12345) hillest<-c(rep(1.1,100*4*3)+rnorm(100*4*3,sd=0.2),        rep(1.9,100*4*3)+rnorm(100*4*3,sd=0.2)) rep<-rep(1:100,4*3*2) process<-rep(rep(c("Process 1","Process 2","Process 3","Process 4"),each=100),3*2) memorypar<-rep(rep(c("0.1","0.2","0.3"),each=4*100),2) tailindex<-rep(c("1.1","1.9"),each=3*4*100) ex5<-data.frame(hillest=hillest,rep=rep,process=process,memorypar=memorypar, tailindex=tailindex) stat_sum_df <- function(fun, geom="crossbar", ...) {stat_summary(fun.data=fun, geom=geom, ...) }  dodge <- position_dodge(width=0.9)  p<- ggplot(ex5,aes(x=tailindex ,y=hillest,color=memorypar))  p<- p + facet_wrap(~process,nrow=2) + geom_jitter(position=dodge) +geom_boxplot(position=dodge)   p 
like image 953
user1381239 Avatar asked May 08 '12 05:05

user1381239


People also ask

What does position Dodge do in ggplot2?

Dodging preserves the vertical position of an geom while adjusting the horizontal position. position_dodge() requires the grouping variable to be be specified in the global or geom_* layer.

What does position jitter do in R?

The jitter geom is a convenient shortcut for geom_point(position = "jitter") . It adds a small amount of random variation to the location of each point, and is a useful way of handling overplotting caused by discreteness in smaller datasets.

What is Position_jitterdodge?

position_jitterdodge.Rd. This is primarily used for aligning points generated through geom_point() with dodged boxplots (e.g., a geom_boxplot() with a fill aesthetic supplied).


1 Answers

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

like image 199
Didzis Elferts Avatar answered Oct 17 '22 03:10

Didzis Elferts