2

我在 R 中有一个图,我试图在 ggplot2 中复制它。我有以下代码:

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)

在此处输入图像描述

我试图在 ggplot2 中复制这个情节,这是我能得到的最接近的情节。

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

在此处输入图像描述

4

1 回答 1

5

你没有正确匹配你的参数。的签名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 限制。

于 2017-07-19T21:26:36.893 回答