如何绘制相对于每个方面的百分比的多面直方图(而非条形图)? [英] How to plot faceted histogram (not bar charts) with percents relative to each facet?

查看:114
本文介绍了如何绘制相对于每个方面的百分比的多面直方图(而非条形图)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管有几条关于如何使用条形图各个方面的百分比百分比的SO帖子,但我看不到任何柱状图显示如何做到这一点.有可能这样做吗?

While there are several SO posts on how to use percents scaled within each facet of a bar chart, I don't see any that show how to do that in a histogram. Is it possible to so do?

这是我研究过的两篇文章:

Here are two posts I researched:

SO帖子1:获得反映各个方面的比例尺ggplot2 的最新答案表明该解决方案不再适用于ggplot2的较新版本,并建议使用stat_count(),但没有给出示例.

SO post 1: Obtaining Percent Scales Reflective of Individual Facets with ggplot2 Last answer on this post indicates solution no longer working on newer versions of ggplot2 and suggests use of stat_count() but does not give example.

SO post2:在多面ggplot中的y实验室百分比条形图?

SO post2: percentage on y lab in a faceted ggplot barchart?

下面的代码创建一个直方图,其中百分比在所有方面(即所有小节的总和为100%)而不是在每个方面中进行缩放.

The following code creates a histogram with the percents scaled across all facets (i.e. sum of all bars is 100%) rather than within each facet.

ggplot(iris, aes(Sepal.Width, y=(..count..)/sum(..count..))) + geom_histogram(bins=2) +
  facet_grid(~Species) + scale_y_continuous(labels = scales::percent)

是否有一种方法可以在每个方面进行缩放?如果没有,转换为条形图的有效策略是什么?如果必须走那条路,我可以使用cut()创建因子指标的因子,然后在构面变量的每个级别内计算因子频率(使用dplyr::count()?),然后使用geom_bar().似乎令人费解.我怀疑有geom_histogram()解决方案.

Is there a way to scale within each facet? If not, what would be an efficient strategy of converting to bar chart? If I had to go that route, I could use cut() to create a factor of bin indicators, then calculate bin frequencies within each level of facet variable (using dplyr::count()?), then use geom_bar(). Seems convoluted. I suspect there is a geom_histogram() solution.

感谢您的任何想法...

Thanks for any thoughts ...

推荐答案

@aosmith指出,其中一篇已研究帖子的答案使用了..PANEL ..内部变量.根据这一建议,下面的更新代码确实有效,尽管具有更复杂的y映射.

@aosmith pointed out that one answer in one of the researched posts makes use of the ..PANEL.. internal variable. Taking that suggestion, the updated code below does work, albeit with more complex y-mapping.

ggplot(iris, aes(Sepal.Width, y=(..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..])) +
  geom_histogram(bins=2) + facet_grid(~Species) + 
  scale_y_continuous(labels = scales::percent) + labs(y='% within facet')

但是,在这篇SO帖子中,Hadley Wickham建议不要以这种方式使用..PANEL ..(和其他内部变量).他建议在ggplot之外进行聚合.因此,也许该问题的答案是,您可以使用故意未记录的"..PANEL .."变量,但要注意使用这种类型的功能的风险,该功能很容易在新版本中进行更改而无需解释.

However, in this SO post, Hadley Wickham advises against using ..PANEL.. (and other internal variables) in this manner. He suggests doing the aggregation outside ggplot. So perhaps the answer to the question is that you can use the intentionally undocumented '..PANEL..' variable but beware the risk of using that type of functionality, which is susceptible to change in new releases without explanation.

也许有人可以请求一项功能,以将构面的密度缩放比例控制为总体",按列",按行",按构面".

Perhaps someone can request a feature that controls the density scaling of facets to 'overall', 'by col', 'by row', 'by facet'.

这篇关于如何绘制相对于每个方面的百分比的多面直方图(而非条形图)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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