带有多个密度的图例

问题描述 投票:1回答:2

我有以下代码:

x <- seq(-25,25, by = .01)
plot(x, dunif(x,-10,10), type = 'l', ylab = 'probability', lwd = 2)
lines(x, dnorm(x,0,10), col = 'blue', lwd = 2)
lines(x, dstudent_t(x,4,0,10), col = 'red', lwd = 2)
lines(x, dcauchy(x,0,10), col = 'green', lwd = 2)
legend(1, 95, legend=c("Uniform (-10,10)", "Normal (0,10)", "Student t (4,0,10)", "Cauchy (0, 10)"),
       col=c("black", "red", "blue", "green"), lty=1:2, cex=0.8)

这给了我以下情节:

Plot

但是图例没有显示,我不确定错误在哪里。

r plot data-visualization probability-density
2个回答
1
投票

尝试

legend(1, 0.05, legend=c("Uniform (-10,10)", "Normal (0,10)", "Student t (4,0,10)", "Cauchy (0, 10)"),
   col=c("black", "red", "blue", "green"), lty=1:2, cex=0.8)

您在y=95调用中的legend值超出了y轴范围。


0
投票

@@ jay.sf根据您的建议,图例起作用了!但是它在图例中显示的行有时是虚线,有时不是,我如何将其更改为图例中所有发行版的简单行? (至于统一和正常)

x <- seq(-25,25, by = .01)
plot(x, dunif(x,-10,10), type = 'l', ylab = 'probability', lwd = 2)
lines(x, dnorm(x,0,10), col = 'blue', lwd = 2)
lines(x, dstudent_t(x,4,0,10), col = 'red', lwd = 2)
lines(x, dcauchy(x,0,10), col = 'green', lwd = 2)
legend("topleft", legend=c("Uniform (-10,10)", "Normal (0,10)", "Student t (4,0,10)", "Cauchy (0, 10)"),
       col=c("black", "red", "blue", "green"), lty=1:2, cex=0.8)

Plot

© www.soinside.com 2019 - 2024. All rights reserved.