23

我正在使用 R 和 Latex 一起绘制一些情节,并试图为所有人制作一个共同的传说。

我在同一页面上有六个单独的图。我在 R 中分别制作了每个图,然后在 Latex 中使用 \includegraphics 将它们显示在同一页面上。

每个图表都有相同的图例信息,所以我不想在每个图中都有一个图例,我希望在页面底部显示一个水平图例。不幸的是,我无法弄清楚如何在没有情节的情况下制作传奇。一旦我有了一个单独的图例图像,我就会知道如何使用 Latex 将它包含在页面底部。

我试图用来制作图例的代码是

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)

但是,字体太小,图例框的一侧被切断。

4

3 回答 3

33

我所说的一个简单的例子:

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)

在此处输入图像描述

于 2012-04-30T21:44:18.100 回答
5

尝试这个,

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)
于 2012-04-30T21:32:35.293 回答
4

修改代码仅par用于对齐底部的图例。

   n <- 6
   par(oma = c(4,1,1,1), mfrow = c(2, 3), mar = c(2, 2, 1, 1))
   for (i in 1:n){
      plot(runif(5),runif(5),xlab = '',ylab = '')
   }
   par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
   plot(0, 0, type = 'l', bty = 'n', xaxt = 'n', yaxt = 'n')
   legend('bottom',legend = c("Fabricated Metal", "Iron and Steel", "Paper", "Beverages", "Tobacco"), col = c("blue","black", "green", "orange", "pink"), lwd = 5, xpd = TRUE, horiz = TRUE, cex = 1, seg.len=1, bty = 'n')
   # xpd = TRUE makes the legend plot to the figure

在此处输入图像描述

于 2020-05-13T21:47:33.407 回答