在 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)情节(x,a,lty = 5,col =蓝色",lwd = 3)线(x,b,lty = 2,col =红色")线(x,g,lty = 4,col =绿色")
我什至试图在每个平均日志和标准偏差日志的右上方添加图例,但它对我不起作用.我想知道是否有人可以指导我解决这个问题.
您还可以阅读 ?plotmath
以添加表达式.尝试将上面的 legend
参数更改为:
legend = c(expression(ln(y) %~% N(0,1)),表达式(ln(y) %~% N(0,1.5)),表达式(ln(y) %~% N(1.5,0.2)))
I am trying to generate a plot for Lognormal Probability Density in R, with 3 different means log and standards deviation log. I have tried the following, but my graph is so ugly and does not look good at all.
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")
I even was trying to add legend on the right top for each mean log and standard deviation log, but it would not work with me. I was wondering if someone could guide me out with that.
There is really nothing wrong in your code. You just forgot to:
- use
type = "l"
inplot
; - set a good
ylim
to hold all lines.
Here is a simple solution with matplot
:
matplot(x, cbind(a,b,g), type = "l", ylab = "density", main = "log-normal",
col = 1:3, lty = 1:3)
To add legend, use
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)
You can also read ?plotmath
for adding expressions. Try changing the legend
argument above to:
legend = c(expression(ln(y) %~% N(0,1)),
expression(ln(y) %~% N(0,1.5)),
expression(ln(y) %~% N(1.5,0.2)))
这篇关于在 R 中绘制对数正态概率密度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!