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

Plot Lognormal Probability Density in R

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

1
2
3
4
5
6
7
 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")

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

图表的右上角


您的代码确实没有任何问题。你只是忘了:

  • plot 中使用 type ="l"
  • 设置一个好的 ylim 来保存所有行。

这是一个使用 matplot:

的简单解决方案

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

要添加图例,请使用

1
2
3
4
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)

enter

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