使用ggplot2在直方图上绘制正常曲线:代码在0处生成直线 [英] Plotting normal curve over histogram using ggplot2: Code produces straight line at 0
问题描述
这个论坛为我生成代码提供了很多帮助,我希望能够返回一个覆盖其经验正态曲线的特定变量的直方图。我用ggplot2和stat_function来编写代码。
不幸的是,代码生成了一个具有正确直方图的曲线图,但正常曲线是一条直线为零(由以下代码生成的红色曲线)。
<对于这个最小的例子,我使用了mtcars数据集 - 我的原始数据集观察到ggplot和stat_function的相同行为。
这是代码被写入和使用:
library(ggplot2)
mtcars
hist_staff< - ggplot(mtcars,aes(x = mtcars $ mpg))+
geom_histogram(binwidth = 2,color =black,aes(fill = ..count ..))+
scale_fill_gradient(Count,low =#DCDCDC ,高=#7C7C7C)+
stat_function(fun = dnorm,color =red)
print(hist_staff)
我也试着指定dnorm:
stat_function(fun = dnorm(mtcars $ mpg,mean = mean(mtcars $ mpg),sd = sd(mtcars $ mpg))
那也没有成功 - 返回的错误消息说明了这一点在参数不是数字。
我希望你们能帮助我!感谢您的提前!
Best,Jannik
曲线和直方图在不同的比例尺上,并且您没有检查 stat_function
上的帮助页面,否则您将参数放在列表
,如清单所示。您还没有在您的初始 ggplot
调用中执行 aes
。我诚恳地建议打出更多的教程和书籍(或者至少帮助页面),同时学习ggplot零碎。
修复 stat_function
arg问题和 ggplot``aes
问题,您需要解决y轴比例差异问题。为此,您需要将直方图的y切换为使用基础 stat_bin
计算数据框中的密度:
library(ggplot2)
gg < - ggplot(mtcars,aes(x = mpg))
gg < - gg + geom_histogram(binwidth = 2,color =black,
aes(y = .. density ..,fill = .. count ..))
gg < - gg + scale_fill_gradient(Count ,low =#DCDCDC,high =#7C7C7C)
gg < - gg + stat_function(fun = dnorm,
color =red,
args = list mean = mean(mtcars $ mpg),
sd = sd(mtcars $ mpg)))
gg
this forum already helped me a lot for producing the code, which I expected to return a histogram of a specific variable overlayed with its empirical normal curve. I used ggplot2 and stat_function to write the code. Unfortunately, the code produced a plot with the correct histogram but the normal curve is a straight line at zero (red line in plot produced by the following code).
For this minimal example I used the mtcars dataset - the same behavior of ggplot and stat_function is observed with my original data set.
This is the code is wrote and used:
library(ggplot2)
mtcars
hist_staff <- ggplot(mtcars, aes(x = mtcars$mpg)) +
geom_histogram(binwidth = 2, colour = "black", aes(fill = ..count..)) +
scale_fill_gradient("Count", low = "#DCDCDC", high = "#7C7C7C") +
stat_function(fun = dnorm, colour = "red")
print(hist_staff)
I also tried to specify dnorm:
stat_function(fun = dnorm(mtcars$mpg, mean = mean(mtcars$mpg), sd = sd(mtcars$mpg))
That did not work out either - an error message returned stating that the arguments are not numerical.
I hope you people can help me! Thanks a lot in advance!
Best, Jannik
Your curve and histograms are on different y scales and you didn't check the help page on stat_function
, otherwise you'd've put the arguments in a list
as it clearly shows in the example. You also aren't doing the aes
right in your initial ggplot
call. I sincerely suggest hitting up more tutorials and books (or at a minimum the help pages) vs learn ggplot piecemeal on SO.
Once you fix the stat_function
arg problem and the ggplot``aes
issue, you need to tackle the y axis scale difference. To do that, you'll need to switch the y for the histogram to use the density from the underlying stat_bin
calculated data frame:
library(ggplot2)
gg <- ggplot(mtcars, aes(x=mpg))
gg <- gg + geom_histogram(binwidth=2, colour="black",
aes(y=..density.., fill=..count..))
gg <- gg + scale_fill_gradient("Count", low="#DCDCDC", high="#7C7C7C")
gg <- gg + stat_function(fun=dnorm,
color="red",
args=list(mean=mean(mtcars$mpg),
sd=sd(mtcars$mpg)))
gg
这篇关于使用ggplot2在直方图上绘制正常曲线:代码在0处生成直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!