ggplot2 - 抖动和位置躲闪在一起

use*_*239 29 r ggplot2

我试图重新从GGPLOT2研讨会上图http://dl.dropbox.com/u/42707925/ggplot2/ggplot2slides.pdf.

在这种情况下,我试图生成示例5,抖动的数据点受闪避.当我运行代码时,这些点以正确的线为中心,但没有抖动.

这是代码直接来自演示文稿.

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
Run Code Online (Sandbox Code Playgroud)

Did*_*rts 56

ggplot2版本中,1.0.0有一个新的位置命名position_jitterdodge()为这种情况.这个位置应该在里面使用,geom_point()并且应该在fill=里面aes()用来显示哪个变量来躲避你的数据.dodge.width=应该使用控制躲避参数的宽度.

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))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


San*_*att 33

编辑:使用ggplot2版本1.0.0 有一个更好的解决方案position_jitterdodge.请参阅@Didzis Elferts的回答.请注意,dodge.width控制闪避jitter.width的宽度并控制抖动的宽度.

我不确定代码是如何在pdf中生成图形的.

但这样的事情会让你接近你所追求的东西吗?

我转换tailindexmemorypar数字; 把它们加在一起; 结果是geom_jitter图层的x坐标.这可能是一种更有效的方法.另外,我想看看如何闪避geom_boxplotgeom_jitter,且无抖动,会产生在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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述