如何在r中一起绘制直方图和pdf

use*_*865 1 plot r histogram probability-density

我想将拟合模型的 PDFgamma(lambda,k)与直方图叠加。我写的 :

hist(pressure)
curve(dgamma(x, lambda, k), add=TRUE, col="red")
Run Code Online (Sandbox Code Playgroud)

但我对“x”的值是什么感到困惑。有人帮忙吗?

bar*_*nus 6

x <- rgamma(100,2,1) #sample
h <- hist(x, plot=FALSE) #generate hist
plot(h, col="grey") #plot hist
xlines <-seq(min(h$breaks),max(h$breaks),length.out=100) #seq of x for pdf
lines(x = xlines,y=dgamma(xlines,2,1) *length(x)*diff(h$breaks)[1])
Run Code Online (Sandbox Code Playgroud)

PDF 直方图