简体   繁体   English

ggplot2 - 抖动和位置闪避一起

[英]ggplot2 - jitter and position dodge together

I am trying to recreate a figure from a GGplot2 seminar http://dl.dropbox.com/u/42707925/ggplot2/ggplot2slides.pdf .我正在尝试从 GGplot2 研讨会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.在这种情况下,我试图生成示例 5,其中抖动数据点会受到闪避。 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

In ggplot2 version 1.0.0 there is new position named position_jitterdodge() that is made for such situation.ggplot2版本1.0.0有一个名为position_jitterdodge()新位置是为这种情况而设计的。 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.这个位置应该在geom_point()内部使用,并且应该在aes()内部使用fill=来显示通过哪个变量来躲避您的数据。 To control the width of dodging argument dodge.width= should be used.要控制躲避参数的宽度,应使用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))

在此处输入图片说明

EDIT : There is a better solution with ggplot2 version 1.0.0 using position_jitterdodge .编辑ggplot2版本 1.0.0 使用position_jitterdodge有更好的解决方案。 See @Didzis Elferts' answer.请参阅@Didzis Elferts 的回答。 Note that dodge.width controls the width of the dodging and jitter.width controls the width of the jittering.注意dodge.width控制着躲避的宽度, jitter.width控制着抖动的宽度。

I'm not sure how the code produced the graph in the pdf.我不确定代码是如何在 pdf 中生成图表的。

But does something like this get you close to what you're after?但是这样的事情会让你接近你所追求的吗?

I convert tailindex and memorypar to numeric;我将tailindexmemorypar转换为数字; add them together;将它们加在一起; and the result is the x coordinate for the geom_jitter layer.结果是geom_jitter层的 x 坐标。 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.另外,我想看看如何躲避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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM