我一起使用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)
但是,字体太小而且图例框的侧面被切掉了。
答案 0 :(得分:24)
我所谈论的一个简单例子:
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)
答案 1 :(得分: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)
答案 2 :(得分:2)
仅使用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