本文介绍了ggplot2:带正态曲线的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 ggplot 2 在我的直方图上叠加一条正态曲线.

I've been trying to superimpose a normal curve over my histogram with ggplot 2.

我的公式:

data <- read.csv (path...)

ggplot(data, aes(V2)) +
  geom_histogram(alpha=0.3, fill='white', colour='black', binwidth=.04)

我尝试了几件事:

+ stat_function(fun=dnorm)

....没有改变任何东西

....didn't change anything

+ stat_density(geom = "line", colour = "red")

...在 x 轴上给了我一条直线红线.

...gave me a straight red line on the x-axis.

+ geom_density()

对我不起作用,因为我想将频率值保留在 y 轴上,并且不需要密度值.

doesn't work for me because I want to keep my frequency values on the y-axis, and want no density values.

有什么建议吗?

预先感谢您提供任何提示!

Thanks in advance for any tips!

找到解决方案!

+geom_density(aes(y=0.045*..count..), colour="black", adjust=4)

推荐答案

我想我明白了:

set.seed(1)
df <- data.frame(PF = 10*rnorm(1000))
ggplot(df, aes(x = PF)) +
    geom_histogram(aes(y =..density..),
                   breaks = seq(-50, 50, by = 10),
                   colour = "black",
                   fill = "white") +
stat_function(fun = dnorm, args = list(mean = mean(df$PF), sd = sd(df$PF)))

这篇关于ggplot2:带正态曲线的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 21:41