R中的相对频率直方图,ggplot [英] Relative frequency histogram in R, ggplot
本文介绍了R中的相对频率直方图,ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用 lattice
包在R中绘制相对频率直方图:
I can draw relative frequency histogram in R, using lattice
package:
a <- runif(100)
library(lattice)
histogram(a)
我想在 ggplot
中获得相同的图形.我尝试过
I want to get the same graph in ggplot
. I tried
dt <- data.frame(a)
ggplot(dt, aes(x = a)) +
geom_bar(aes(y = ..prop..))+
scale_y_continuous(labels=percent)
但是它不能那样工作.我应该在代码中更改什么?对我来说,在图形之前计算相对频率不是一个选择.
but it doesn't work like that. What I should change in the code? Calculating relative frequency before graph is not an option for me.
推荐答案
您想要的是直方图,而不是条形图,所以:
You want a histogram, not a barplot, so:
ggplot(dt, aes(x = a)) +
geom_histogram(aes(y = stat(count) / sum(count)), bins = 8) +
scale_y_continuous(labels = scales::percent)
晶格
:
ggplot2
:
您会看到,两种算法的分箱算法工作稍有不同.
You can see that the binning algorithm works slightly different for the two packages.
这篇关于R中的相对频率直方图,ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文