2017-07-19 53 views
1

我在R中有一个我试图在ggplot2中复制的情节。我有以下代码:如何绘制ggplot2中的Gamma分布

theta = seq(0,1,length=500) 
post <- dgamma(theta,0.5, 1) 
plot(theta, post, type = "l", xlab = expression(theta), ylab="density", lty=1, lwd=3) 

enter image description here

我试图复制在GGPLOT2该地块,这是我能得到的最接近。

df=data_frame(post,theta) 
ggplot(data=df,aes(x=theta))+ 
    stat_function(fun=dgamma, args=list(shape=1, scale=.5)) 

enter image description here

回答

4

你没有正确地匹配您的参数。的dgamma签名是

dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE) 

所以当你打电话

dgamma(theta, 0.5, 1) 

dgamma(theta, shape=0.5, rate=1) 

这意味着你会翻译ggplot作为

ggplot(data=df,aes(x=theta))+ 
    stat_function(fun=dgamma, args=list(shape=0.5, rate=1)) 

ÿ如果你喜欢scale_y_continuous(limits=c(0,12))或类似的东西,你也可以调整y值。

+0

感谢您的帮助! – Alex