缩放geom_density以使geom_bar与y上的百分比匹配 [英] Scale geom_density to match geom_bar with percentage on y
问题描述
由于我对数学
密度和bar y值匹配,但缩放比例不合理.我想要在y轴上显示百分比,而不是密度.
一些新尝试.我们从修改条形图开始,以显示百分比而不是计数:
gg = ggplot2 :: ggplot(iris,aes(Sepal.Length))+geom_bar(aes(y = ..count ../sum(.. count ..)))+scale_y_continuous(name =%",labels = scales :: percent)
然后,我们尝试在其中添加geom_density并以某种方式使其正确缩放:
gg + geom_density()
gg + geom_density(aes(y = .. count ..))
gg + geom_density(aes(y = .. scaled ..))
gg + geom_density(aes(y = .. density ..))
与第一个相同.
gg + geom_density(aes(y = ..count ../sum(.. count ..))))
gg + geom_density(aes(y = ..count ../n))
似乎偏离了大约10倍...
gg + geom_density(aes(y = ..count ../n/10))
与:
gg + geom_density(aes(y = ..density ../10))
但是临时插入数字似乎是个坏主意.
一个有用的技巧是检查绘图的计算值.如果将其保存,则通常不会将其保存在对象中.但是,可以使用:
gg_data = ggplot_build(gg + geom_density())gg_data $ data [[2]]%>%视图
由于我们知道x = 6周围的密度拟合应该约为.04(4%),因此我们可以四处寻找ggplot2计算得出的值,使我们到达那里,而我唯一看到的就是密度/10.>
如何使 geom_density
适合缩放到与修改后的 geom_bar
相同的y轴?
奖金问题:金条的分组为什么不同?当前函数在小节之间没有空格.
这是一个简单的解决方案:
library(scales)#!重要的库(ggplot2)ggplot(iris,aes(Sepal.Length))+stat_bin(aes(y = .. density ..),breaks = seq(min(iris $ Sepal.Length),max(iris $ Sepal.Length),by = .1),color ="white")+geom_line(stat ="density",size = 1)+scale_y_continuous(labels = percent,name ="percent")+theme_classic()
输出:
Since I was confused about the math last time I tried asking this, here's another try. I want to combine a histogram with a smoothed distribution fit. And I want the y axis to be in percent.
I can't find a good way to get this result. Last time, I managed to find a way to scale the geom_bar
to the same scale as geom_density
, but that's the opposite of what I wanted.
My current code produces this output:
ggplot2::ggplot(iris, aes(Sepal.Length)) +
geom_bar(stat="bin", aes(y=..density..)) +
geom_density()
The density and bar y values match up, but the scaling is nonsensical. I want percentage on the y axes, not well, the density.
Some new attempts. We begin with a bar plot modified to show percentages instead of counts:
gg = ggplot2::ggplot(iris, aes(Sepal.Length)) +
geom_bar(aes(y = ..count../sum(..count..))) +
scale_y_continuous(name = "%", labels=scales::percent)
Then we try to add a geom_density to that and somehow get it to scale properly:
gg + geom_density()
gg + geom_density(aes(y=..count..))
gg + geom_density(aes(y=..scaled..))
gg + geom_density(aes(y=..density..))
Same as the first.
gg + geom_density(aes(y = ..count../sum(..count..)))
gg + geom_density(aes(y = ..count../n))
Seems to be off by about factor 10...
gg + geom_density(aes(y = ..count../n/10))
same as:
gg + geom_density(aes(y = ..density../10))
But ad hoc inserting numbers seems like a bad idea.
One useful trick is to inspect the calculated values of the plot. These are not normally saved in the object if one saves it. However, one can use:
gg_data = ggplot_build(gg + geom_density())
gg_data$data[[2]] %>% View
Since we know the density fit around x=6 should be about .04 (4%), we can look around for ggplot2-calculated values that get us there, and the only thing I see is density/10.
How do I get geom_density
fit to scale to the same y axis as the modified geom_bar
?
Bonus question: why are the grouping of the bars different? The current function does not have spaces in between bars.
Here is an easy solution:
library(scales) # ! important
library(ggplot2)
ggplot(iris, aes(Sepal.Length)) +
stat_bin(aes(y=..density..), breaks = seq(min(iris$Sepal.Length), max(iris$Sepal.Length), by = .1), color="white") +
geom_line(stat="density", size = 1) +
scale_y_continuous(labels = percent, name = "percent") +
theme_classic()
Output:
这篇关于缩放geom_density以使geom_bar与y上的百分比匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!