【发布时间】:2021-03-09 09:46:27
【问题描述】:
我创建了一个直方图来显示连环杀手首次被杀的年龄密度,并尝试在其上叠加一个概率密度函数。但是,当我在 ggplot2 中使用 geom_density() 函数时,我得到的密度函数看起来太小了(面积
#Histograms for Age of First Kill:
library(ggplot2)
AFKH <- ggplot(df, aes(AgeFirstKill,fill = cut(AgeFirstKill, 100))) +
geom_histogram(aes(y=..count../sum(..count..)), show.legend = FALSE, binwidth = 3) + # density wasn't working, so had to use the ..count/../sum(..count..)
scale_fill_discrete(h = c(200, 10), c = 100, l = 60) + # c =, for color, and l = for brightness, the #h = c() changes the color gradient
theme(axis.title=element_text(size=22,face="bold"),
plot.title = element_text(size=30, face = "bold"),
axis.text.x = element_text(face="bold", size=14),
axis.text.y = element_text(face="bold", size=14)) +
labs(title = "Age of First kill",x = "Age of First Kill", y = "Density")+
geom_density(aes(AgeFirstKill, y = ..density..), alpha = 0.7, fill = "white",lwd =1, stat = "density")
AFKH
【问题讨论】:
-
第二个图中的直方图错误,而不是天性:列的高度太大了。密度似乎还可以,因为它与第一个图相同。
-
可以给我们一个minimal reproducible example吗?如果您希望直方图和密度匹配,您可能应该使用
..density..,它也考虑了 bin 宽度
标签: r ggplot2 statistics histogram kernel-density