R中多个图的通用图例 [英] Common legend for multiple plots in R
问题描述
我正在一起使用R和Latex绘制一些图,并试图为它们全部创建一个共同的图例.
I am using R and Latex together to draw some plots and am trying to make a common legend for all of them.
我在同一页面上有六个单独的地块.我在R中分别制作了每个图,然后在Latex中使用\ includegraphics将它们显示在同一页面上.
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.
每个图都有相同的图例信息,因此我不想在每个绘图中都有图例,而是希望在页面底部显示一个水平图例.不幸的是,我不知道如何在没有情节的情况下制作图例.一旦有了图例的单独图像,我将知道如何使用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.
推荐答案
一个我正在谈论的简单示例:
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)
这篇关于R中多个图的通用图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!