ggplot2 - 抖动和位置一起闪避 [英] ggplot2 - jitter and position dodge together

查看:192
本文介绍了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)
进程< -rep(rep(c(进程1,进程2,进程3,进程4),每个= 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 < - 函数(fun,geom =crossbar,...){stat_summary(fun.data = fun,geom = geom,...)}

闪避< - position_dodge(width = 0.9)$ (x = tailindex,y = hillest,color = memorypar))
p <-p + facet_wrap(〜process,nrow = 2)+ geom_jitter(position = dodge) + geom_boxplot(position = dodge)
p
> ggplot2
版本1.0.0使用 position_jitterdodge 。请参阅@Didzis Elferts的答案。请注意, dodge.width 控制闪避的宽度, jitter.width 控制抖动的宽度。 p>

我不确定代码如何在PDF中生成图表。



但是,接近你以后的事情?



我将 tailindex memorypar 转换为numeric;将它们加在一起;并且结果是 geom_jitter 图层的x坐标。可能有更有效的方法来做到这一点。另外,我想看看如何避免 geom_boxplot geom_jitter ,并且没有抖动,会产生图

  library(ggplot2)
闪避< - 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(color = memorypar,x = memorypar2),
position = position_jitter(width = 05),alpha = 0.5)+
geom_boxplot(aes(color = memorypar),outlier.colour = NA,position = dodge)+
facet_wrap(〜process,nrow = 2)
p


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

解决方案

EDIT: There is a better solution with ggplot2 version 1.0.0 using position_jitterdodge. See @Didzis Elferts' answer. Note that dodge.width controls the width of the dodging and jitter.width controls the width of the jittering.

I'm not sure how the code produced the graph in the pdf.

But does something like this get you close to what you're after?

I convert tailindex and memorypar to numeric; add them together; and the result is the x coordinate for the geom_jitter layer. 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.

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

这篇关于ggplot2 - 抖动和位置一起闪避的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆