Is it possible to jitter two ggplot geoms in the same way?

前端 未结 3 700
野的像风
野的像风 2020-12-15 20:42

Using position_jitter creates random jitter to prevent overplotting of data points.

In the below I have used the example of baseball statistics to illustrate my prob

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 20:48

    I think so, by setting the seed to be the same in the two instances:

    p=ggplot(baseball,aes(x=round(year,-1),y=sb,color=factor(lg)))
    myseed = 2010
    set.seed(myseed)
    p=p+stat_summary(fun.data="mean_cl_normal",
      position=position_jitter(width=3,height=0))+coord_cartesian(ylim=c(0,40))
    set.seed(myseed)
    p+stat_summary(fun.y=mean,geom="line",
               position=position_jitter(width=3,height=0))
    

    This ensures that the random number generator is sent back to the same starting position as was used in the initial call. However I don't know how you could extract the random increments added to the values.

提交回复
热议问题