如何绘制堆积点直方图? [英] How to plot stacked point histograms?

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

问题描述

ggplot2等效于"dotplot"直方图?用堆积点代替条形图?类似于R中的此解决方案:

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

用点而不是条形图绘制直方图

是否可以在ggplot2中执行此操作?理想情况下,将这些点显示为堆栈,并使用一条淡线显示与这些点拟合"的平滑线(这将形成直方图形状).

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

推荐答案

ggplot2 进行点图链接到手册.

这里是一个例子:

library(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')    

您应该得到这个:

要解决密度问题,您必须添加另一个术语ylim(),以便绘图调用的格式为ggplot() + geom_dotplot() + ylim()

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

更具体地说,您将编写ylim(0, A),其中A是计算1.00密度所需的堆叠点数.在上面的示例中,您能做的最好的事情是看到7.5个点达到了0.50的密度标记.从那里,您可以推断出15个点将达到1.00.

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)

哪个会给你这个:

通常,这种眼球估计适用于点状图,但是您当然可以尝试使用其他值来微调刻度.

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

请注意,更改ylim值不会影响数据的显示方式,只是会更改y轴上的标签.

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

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

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