在ggplot2中分别规格化分面直方图 [英] Normalizing faceted histograms separately in ggplot2

查看:333
本文介绍了在ggplot2中分别规格化分面直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于
将R ggplot中的直方图中的y轴规格化为比例
,但我想补充一点。



通常,I在2x3方面设计中有6个直方图,并且我想分别标准化它们中的每一个。我会尝试在这里创建一个示例数据集来给出一个想法:

  hvalues = c(3,1,3, 2,2,5,1,1,12,1,4,3)
season = c(fall,fall,fall,fall,winter,winter, 冬季,冬季,夏季,夏季,夏季,夏季)
年份= c(第1年,第1年,第2年 ,年1,年1,年2,年2,年1,年1,年2,年2)
group = c 秋季1,秋季1,秋季2,秋季2,冬季1,冬季1,冬季2,冬季2,夏季年份1,夏季1,夏季2,夏季2)
all = data.frame(hvalues,season,year)

使用

  ggplot(all,aes(x = hvalues,group = group))+ 
geom_histogram(aes(y = .. count ../ sum(.. count ..)))+
facet_grid(season〜year)

给出整体比例(即组合所有方面)。我希望每个组方面都归一化为1.在我的实际数据中,hvalues不是整数 - 它们是数字。



我是使用R的新手,真的很感谢一些帮助。

解决方案

解决方案是:

  ggplot(all,aes(x = hvalues))+ 
facet_grid(season〜year,drop = T)+
geom_histogram(aes(y =(.. count ..) )/ tapply(.. count ..,.. PANEL .. sum)[.. PANEL ..]))

我从这个问题< a>



我觉得你的问题可能是那个问题的重复。

My questions is similar to Normalizing y-axis in histograms in R ggplot to proportion but I'd like to add to it a bit.

In general, I have 6 histograms in a 2x3 facet design, and I'd like to normalize each of them separately. I'll try to make a sample data set here to give an idea:

hvalues=c(3,1,3,2,2,5,1,1,12,1,4,3)
season=c("fall","fall","fall","fall","winter","winter","winter","winter","summer","summer","summer","summer")
year=c("year 1","year 1","year 2","year 2","year 1","year 1","year 2","year 2","year 1","year 1","year 2","year 2")
group=c("fall year 1","fall year 1","fall year 2","fall year 2","winter year 1","winter year 1","winter year 2","winter year 2","summer year 1","summer year 1","summer year 2","summer year 2")
all=data.frame(hvalues,season,year)

Using

ggplot(all, aes(x=hvalues,group=group)) + 
geom_histogram(aes(y=..count../sum(..count..))) + 
facet_grid(season ~ year)

gives the proportions overall (i.e. combining all the facets). I'd like each group facet to be normalized to 1. hvalues are not integers in my actual data - they are numerical.

I am a novice using R, and would really appreciate some help. Thanks in advance!

解决方案

The solution is:

ggplot(all, aes(x=hvalues)) +
    facet_grid(season ~ year,drop=T) +
    geom_histogram(aes(y=(..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..]))

I stole this from this question

I feel your question might be a duplicate of that one by the way.

这篇关于在ggplot2中分别规格化分面直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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