如何在R中的ggplot2中绘制堆积点直方图? [英] How to plot stacked point histograms in ggplot2 in R?

查看:488
本文介绍了如何在R中的ggplot2中绘制堆积点直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dotplot直方图的ggplot2相当于什么?用积分而不是酒吧?类似于R中的这个解决方案:

用点代替直方图绘制直方图



在ggplot2中可以做到这一点吗?理想情况下,将点显示为堆栈,并用微弱的线条显示平滑线对这些点适合(这将形成直方图形状)。解决方案

div>

是的, ggplot2 是否有dotplots(顺便说一下,它可能会做所有你可以想象的情节,看看 http://docs.ggplot2.org/current/index.html )。基本上,你想要的是:

  require(ggplot2)
set.seed(789)
x < - data.frame(y = sample(1:20,100,replace = TRUE))
ggplot(x,aes(y))+ geom_dotplot()


为了使其表现得像一个简单的dotplot,我们应该这样做:

  ggplot(x,aes(y))+ geom_dotplot(binwidth = 1,method ='histodot')

你应该得到这个:



ylim() $ c>,这样您的剧情调用将具有 ggplot()+ geom_dotplot()+ ylim()
<更具体地说,你会写 ylim(0,A),其中 A 计算1.00密度所需的点。在上面的例子中,你可以做的最好的是7.5个点到达0.50密度标记。从那里,你可以推断15点将达到1.00。



所以你的新电话看起来像这样:

<$
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'



哪个会给你这个:



通常情况下,这种眼球估计适用于dotplots,但您当然可以尝试其他值来微调你的比例。



注意如何改变ylim值不会影响数据的显示方式,它只会改变y轴上的标签。


What's the ggplot2 equivalent of "dotplot" histograms? With stacked points instead of bars? Similar to this solution in R:

Plot Histogram with Points Instead of Bars

Is it possible to do this in ggplot2? Ideally with the points shown as stacks and a faint line showing the smoothed line "fit" to these points (which would make a histogram shape.)

解决方案

Yes, ggplot2 does dotplots (BTW, it probably does all the plots you can imagine, take a look at http://docs.ggplot2.org/current/index.html). Basically, what you want is this:

require(ggplot2)
set.seed(789)
x <- data.frame(y = sample(1:20, 100, replace = TRUE))
ggplot(x, aes(y)) + geom_dotplot()

In order to make it behave like a simple dotplot, we should do this:

ggplot(x, aes(y)) + geom_dotplot(binwidth=1, method='histodot')    

You should get this:

To address the density issue, you'll have to add another term, ylim(), so that your plot call will have the form ggplot() + geom_dotplot() + ylim()

More specifically, you'll write ylim(0, A), where A will be the number of stacked dots necessary to count 1.00 density. In the example above, the best you can do is see that 7.5 dots reach the 0.50 density mark. From there, you can infer that 15 dots will reach 1.00.

So your new call looks like this:

ggplot(x, aes(y)) + geom_dotplot(binwidth=1, method='histodot') + ylim(0, 15)

Which will give you this:

Usually, this kind of eyeball estimate will work for dotplots, but of course you can try other values to fine-tune your scale.

Notice how changing the ylim values doesn't affect how the data is displayed, it just changes the labels in the y-axis.

这篇关于如何在R中的ggplot2中绘制堆积点直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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