是否有可能以同样的方式抖动两个ggplot几何? [英] Is it possible to jitter two ggplot geoms in the same way?

查看:95
本文介绍了是否有可能以同样的方式抖动两个ggplot几何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用position_jitter可创建随机抖动以防止数据点出现重叠图。

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 problem. When I plot the same data with two layers, the same jitter call jitters the geoms a bit differently. This makes sense because it presumably generates the random jitter independently in the two calls, but yields the problem you can see in my graph below.

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

虽然错误栏点和线指的是相同的数据,他们是脱节 - 行和点不连接。

Although the error bar points and the line refer to same data, they are disjointed—the lines and points do not connect.

有没有解决这个问题的方法?我认为职位闪避可能是答案,但似乎并不适用于这类情节。或者,也许有一些方法可以让mean_cl_normal调用添加行吗?
替代文本http://img339.imageshack.us/img339/1807/screenshot20100702at943.png

Is there a work-around for this? I thought position dodge might be the answer but it doesn't seem to work with these kinds of plots. Alternatively, maybe there's some way to get the mean_cl_normal call to also add the lines? alt text http://img339.imageshack.us/img339/1807/screenshot20100702at943.png

推荐答案

这是目前ggplot2语法的一个弱点 - 除了自己添加抖动之外,没有办法解决它。

This is a weakness in the current ggplot2 syntax - there's no way to work around it except to add the jitter yourself.

或者你可以这样做:

Or you could do something like this:

ggplot(baseball, aes(round(year,-1) + as.numeric(factor(lg)), sb, color = factor(lg))) +
  stat_summary(fun.data="mean_cl_normal") +
  stat_summary(fun.y=mean,geom="line") +
  coord_cartesian(ylim=c(0,40))

这篇关于是否有可能以同样的方式抖动两个ggplot几何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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