Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

Common legend for multiple plots in R

I am using R and Latex together to draw some plots and am trying to make a common legend for all of them.

I have six separate plots on the same page. I made each plot separately in R and then displayed them on the same page using includegraphics in Latex.

Each graph has the same legend information, so rather than having a legend in each plot I would like to have one horizontal legend on display at the bottom of page. Unfortunately, I can't figure out how to make a legend without a plot. Once I have a separate image for the legend, I will know how to include it at the bottom of the page using Latex.

The code I am trying to use to make the legend is

plot(1, type = "n", axes=FALSE, xlab="", ylab="")
plot_colors <- c("blue","black", "green", "orange", "pink")

legend(.6,1.3,legend = c("Fabricated Metal", "Iron and Steel", "Paper", 
"Beverages", "Tobacco"), 
       col=plot_colors, lwd=5, cex=.5, horiz = TRUE)

But, the font is too small and the side of the legend box is cut off.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...