用R ggplot2中的正态曲线覆盖分类数据的直方图

时间:2019-04-24 16:55:24

标签: r ggplot2

尝试将法线曲线覆盖到5个类别变量的直方图中,如下所示:

library(ggplot2)

cats = c("1-lowest", "2-low", "3-avg", "4-better", "5-best")
props <- c(0.01,0.05,0.7,0.15,0.09)
rankings <-  sample( cats, 10000, replace=TRUE, prob=props) 
ggp <-  ggplot(data.frame(rankings), aes(x=rankings, y=..count../sum(..count..))) 
print(ggp+geom_histogram(fill="skyblue3", stat="count"))

我在其他类似的线程基础上尝试了以下方法,但是由于我正在处理类别(与其他线程中的连续数据相比)而失败了

ggp+geom_histogram(fill="skyblue3", stat="count")+
+ stat_function(fun=dnorm, args=list(mean=mean(props), sd=sd(props)), lwd=2, col='tomato3')  

0 个答案:

没有答案