gpt4 book ai didi

r - 在ggplot2中分别标准化多面直方图

转载 作者:行者123 更新时间:2023-12-02 02:17:56 26 4
gpt4 key购买 nike

我的问题类似于 Normalizing y-axis in histograms in R ggplot to proportion但我想补充一点。

一般来说,我在 2x3 构面设计中有 6 个直方图,我想分别对每个直方图进行标准化。我将尝试在此处制作一个示例数据集来给出一个想法:

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)

使用

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

我从这个question偷了这个

顺便说一下,我觉得你的问题可能与那个问题重复。

关于r - 在ggplot2中分别标准化多面直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16339204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com