r 频率计数叠加直方图和密度图

r frequency counts on overlayed histogram and density plot

我有兴趣在密度图覆盖的直方图上添加频率计数。 This question is similar to a question already posted on SO 来自其他用户。我尝试了为该问题提供的解决方案,但没有用。

这是我的测试数据集

df <- data.frame(cond = factor( rep(c("A","B"), each=200)), 
                 rating = c(rnorm(200), rnorm(200, mean=.8)))

这将绘制一个带有计数的直方图

ggplot(df, aes(x=rating)) + geom_histogram(binwidth=.5, colour="black", fill="white")

这将绘制这样的密度图

ggplot(df, aes(x=rating)) + geom_density()

我试着把两者结合起来,

ggplot(df, aes(x=rating)) + geom_histogram(aes(y=..count..), binwidth=.5, colour="black", fill="white") + geom_density(alpha=.2, fill="#FF6666")

覆盖的密度图消失了。

我试过这个方法

ggplot(df, aes(x=rating)) + geom_histogram(binwidth=0.5, colour="black", fill="white") + stat_bin(aes(y=..count.., ,binwidth=0.5,label=..count..), geom="text", vjust=-.5) + geom_density(alpha=.2, fill="#FF6666")

这几乎没问题,但没有显示密度图并覆盖了我的绑定宽度值(挠头)。

如何保留带有计数的直方图并显示重叠的密度图?

这将解决您的问题。该问题与 binwidth 有关您需要通过计数和 bin 宽度调整密度图的 y 值,因为密度始终 = 1.

library(ggplot2)

set.seed(1234)

df <- data.frame(cond = factor( rep(c("A","B"), each=200)), 
                 rating = c(rnorm(200), rnorm(200, mean=.8)))

ggplot(df, aes(x=rating)) + 
  geom_histogram(aes(y = ..count..), binwidth = 0.5, colour = "black", fill="white") +
  stat_bin(aes(y=..count.., binwidth = 0.5,label=..count..), geom="text", vjust=-.5) + 
  geom_density(aes(y = ..count.. * 0.5), alpha=.2, fill="#FF6666")


# This is more elegant: using the built-in computed variables for the geom_ functions


ggplot(df, aes(x = rating)) + 
  geom_histogram(aes(y = ..ncount..), binwidth = 0.5, colour = "black", fill="white") +
  stat_bin(aes(y=..ncount.., binwidth = 0.5,label=..count..), geom="text", vjust=-.5) + 
  geom_density(aes(y = ..scaled..), alpha=.2, fill="#FF6666")

这导致: