Common legend for multiple plots in R

假装没事ソ 提交于 2019-11-27 00:40:28

A simple example of what I was talking about:

m <- matrix(c(1,2,3,4,5,6,7,7,7),nrow = 3,ncol = 3,byrow = TRUE)

layout(mat = m,heights = c(0.4,0.4,0.2))

for (i in 1:6){
    par(mar = c(2,2,1,1))
    plot(runif(5),runif(5),xlab = "",ylab = "")
}


plot(1, type = "n", axes=FALSE, xlab="", ylab="")
plot_colors <- c("blue","black", "green", "orange", "pink")
legend(x = "top",inset = 0,
        legend = c("Fabricated Metal", "Iron and Steel", "Paper","Beverages", "Tobacco"), 
        col=plot_colors, lwd=5, cex=.5, horiz = TRUE)

Try this,

plot_colors <- c("blue","black", "green", "orange", "pink")
text <- c("Fabricated Metal", "Iron and Steel", "Paper", 
"Beverages", "Tobacco")
plot.new()
par(xpd=TRUE)
legend("center",legend = text, text.width = max(sapply(text, strwidth)),
       col=plot_colors, lwd=5, cex=1, horiz = TRUE)
par(xpd=FALSE)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!