为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕仙森 2019-12-27 10:19:47
我正在尝试从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
查看完整描述

2 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

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


我不确定代码如何在pdf中产生图形。


但是,这样的事情会使您接近所追求的吗?


我将tailindex和转换memorypar为数字;将它们加在一起;结果是该geom_jitter图层的x坐标。可能有一种更有效的方法。另外,我想看看如何避开geom_boxplot和geom_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


查看完整回答
反对 回复 2019-12-27
?
HUH函数

TA贡献1836条经验 获得超4个赞

在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))


查看完整回答
反对 回复 2019-12-27
  • 2 回答
  • 0 关注
  • 887 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信