在 R 中绘制对数正态概率密度

Plot Lognormal Probability Density in R

我正在尝试在 R 中生成对数正态概率密度图,其中包含 3 种不同的均值对数和标准差对数。我尝试了以下方法,但我的图表太丑了,看起来一点也不好。

 x<- seq(0,10,length = 100)
 a <- dlnorm(x, meanlog = 0, sdlog = 1, log = FALSE)
 b <- dlnorm(x, meanlog = 0, sdlog = 1.5, log = FALSE)
 g <- dlnorm(x, meanlog = 1.5, sdlog = 0.2, log = FALSE)
 plot(x,a, lty=5, col="blue", lwd=3)
 lines(x,b, lty=2, col = "red")
 lines(x,g, lty=4, col = "green")

我什至试图在右上角为每个平均日志和标准偏差日志添加图例,但它对我不起作用。我想知道是否有人可以指导我解决这个问题。

Right top of the graph

你的代码真的没有错。你只是忘了:

  • plot中使用type = "l"
  • 设置好ylim保持所有行

这是一个简单的解决方案 matplot:

matplot(x, cbind(a,b,g), type = "l", ylab = "density", main = "log-normal",
        col = 1:3, lty = 1:3)

要添加图例,请使用

legend("topright",
       legend = c("mu = 0, sd = 1", "mu = 0, sd = 1.5", "mu = 1.5, sd = 0.2"),
       col = 1:3,
       lty = 1:3)

您还可以阅读 ?plotmath 添加表达式。尝试将上面的 legend 参数更改为:

legend = c(expression(ln(y) %~% N(0,1)),
           expression(ln(y) %~% N(0,1.5)),
           expression(ln(y) %~% N(1.5,0.2)))