具有不同标题的 R 图例

R legends with Different Titles

在 R plot 中(不是在 ggplot2 中)如何放置多个不同标题的图例?

plot(sin(1:100), type="l", col="red")
lines(cos(1:100), col="blue", lty=2)
legend("bottom", legend=c("Sin"), fill=c("red"), title="Sin Plot")
legend("bottom", legend=c("Cos"), fill=c("blue"), title="Cos Plot")

第二个传说"Cos"覆盖了第一个!如何将第二个图例放在第一个带有标题的下方?

我不仅在寻找多重传说,而且在寻找具有不同标题的多重传说。比如这个图中:

http://i2.wp.com/www.milanor.net/blog/wp-content/uploads/2015/11/final-1.png?zoom=1.5&w=456

检查这是否是您想要的:

par(xpd=TRUE, mar=c(4.5, 4.5, 1, 6))
plot(sin(1:100), type="l", col="red")
lines(cos(1:100), col="blue", lty=2)
legend(110, 0, legend=c("Sin"), fill=c("red"), title="Sin Plot")
legend(110, -0.5, legend=c("Cos"), fill=c("blue"), title="Cos Plot")

您必须设置 par(xpd=TRUE) 才能禁用裁剪,并在绘图区域外绘制。然后,相应地调整边距,并在您想要的位置手动设置图例。