這已經回答了here和部分here。
如果你想Y軸有頻率計數,那麼正常曲線需要根據觀測值的數量和binwidth進行縮放。
# Simulate some data. Individuals' heights in cm.
n <- 1000
mean <- 165
sd <- 6.6
binwidth <- 2
height <- rnorm(n, mean, sd)
qplot(height, geom = "histogram", breaks = seq(130, 200, binwidth),
colour = I("black"), fill = I("white"),
xlab = "Height (cm)", ylab = "Count") +
# Create normal curve, adjusting for number of observations and binwidth
stat_function(
fun = function(x, mean, sd, n, bw){
dnorm(x = x, mean = mean, sd = sd) * n * bw
},
args = c(mean = mean, sd = sd, n = n, bw = binwidth))

EDIT
或者,對於更靈活的方法,允許使用小面的並且在列出here的方法繪製,創建包含該數據用於正常曲線的單獨的數據集和覆蓋這些。
library(plyr)
dd <- data.frame(
predicted = rnorm(720, mean = 2, sd = 2),
state = rep(c("A", "B", "C"), each = 240)
)
binwidth <- 0.5
grid <- with(dd, seq(min(predicted), max(predicted), length = 100))
normaldens <- ddply(dd, "state", function(df) {
data.frame(
predicted = grid,
normal_curve = dnorm(grid, mean(df$predicted), sd(df$predicted)) * length(df$predicted) * binwidth
)
})
ggplot(dd, aes(predicted)) +
geom_histogram(breaks = seq(-3,10, binwidth), colour = "black", fill = "white") +
geom_line(aes(y = normal_curve), data = normaldens, colour = "red") +
facet_wrap(~ state)
檢查上一個相關的問題,在這裏我已經寫了一個泛型函數疊加在密度圖直方圖這個答案我的。 http://stackoverflow.com/questions/6847450/r-how-to-plot-gumbel-distribution-using-ggplot2s-stat-function/6848958#6848958 – Ramnath
但是,該功能需要在Y軸上的密度值,對吧?我希望保持我的頻率計數!我不想要一個密度圖,而是一個簡單的正態曲線。 – Bloomy
但正常曲線有密度。所以我很困惑。你想要一個具有頻率計數的正態曲線嗎? – Ramnath