我试图在R中绘制直方图,并用来自不同分布的密度覆盖它。它对于常规直方图效果很好,但是我无法将其与ggplot2包配合使用。
a <- dataset$age
现在遵循我的常规直方图的代码:
Histogram_for_age <- hist(a, prob=T, xlim=c(0,80), ylim=c(0,0.055), main="Histogram for age with density lines", xlab="age")
mean <- mean(a)
sd <- sd(a)
现在是密度的线/曲线:
lines(density(dataset$age), col="blue", lwd=2, lty=1)
curve(dnorm(x, mean = mean, sd = sd), add = T, col="red", lwd=2, lty=2)
curve(dgamma(x, shape =mean^2/sd^2, scale = sd^2/mean), add = T, col="goldenrod", lwd=2, lty=3)
和一个传说:
legend("topright",
c("actual distribution of age","gaussian distribution", "gamma distribution"),
lty=c(1,2,3),
lwd=c(2,2,2),col=c("blue","red","goldenrod"), cex=0.65)
到目前为止,这是我尝试过的ggplot2的内容:
ggplot(dataset, aes(x=age)) +
geom_histogram(aes(y=..density..),
colour="black", fill="white") +
geom_density(alpha=.2, fill="lightblue") + stat_function(fun = dgamma, shape=shape)
什么ggplot2参数等效于我的lines()和curve()参数?
使用stat_density
而不是geom_density
这样:
ggplot(dataset, aes(x=age)) +
geom_histogram(aes(y=..density..), colour="black", fill="white") +
stat_density(colour="blue", geom="line", position="identity") +
stat_function(fun=dnorm, args=list(mean=mean(dataset_with_victims$TV_Alter), sd=sd(dataset_with_victims$TV_Alter))) +
stat_function(fun=dgamma, args=list(shape=mean(dataset_with_victims$TV_Alter)^2/sd(dataset_with_victims$TV_Alter)^2, scale=sd(dataset_with_victims$TV_Alter)^2/mean(dataset_with_victims$TV_Alter)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句