繁体   English   中英

如何使用R在标题(外部图)旁边绘制图例?

[英]How do I plot a legend next to my title (outside plot) using R?

我正在使用基本R plot() ,并且想要图例(颜色块和键)显示在我的图的右上方( title()旁边)(使用title()生成)。

最好的方法是什么?

也许您正在寻找这样的东西:

x <- c(1,2,3,4)
y <- c(4,1,3,2)
z <- c(1,2,3,4)
dat <- data.frame(x,y,z)

windows(width = 5, height = 9)        #quartz() on Mac
layout(matrix(c(1,2), 2, 1, byrow = TRUE), heights=c(0.5,1))
par(oma = c(4,3,0,0) + 0.1, mar = c(0,0,1,1) + 0.1)
plot(dat$x, y=rep(1,4), type = "n", axes = F, ylab = "", xlab = "")
legend(x = "bottomright", legend = c("y", "z"), fill = c("blue", "red"))
plot(dat$x, dat$y, type = "n", main = "PLOT")
lines(z, col = "red")
lines(y, col = "blue")

基本上,这将绘制两个图,其中一个图不可见并被缩短,因此显示的只是图例。
您可能还可以调整图例和其他图形参数( ?par )周围的边距,以使布局更好。 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM