【发布时间】:2014-07-24 12:27:44
【问题描述】:
我使用 R 绘制了六个图表。它们被排列成 2 行和 3 列。 在所有这些图表的底部,我想创建一个包含四行的图例,每行旁边都有一个名称,即
**------- aaaaaaa -------bbbbbbbbbbbbbbbbbb -------cccccc --------dddddd **
但是,当我在图例中使用 text.width 函数时,我无法弄清楚如何计算 text.width 的值。正如您在上面看到的,一些图例名称较长(例如 bbbbbbbbbbbbbbbbb)因此需要更多空间。
R代码:
par(mfrow=c(2,3),mar=c(4.5,4,1,1),oma=c(2,0,0,0)) #Setting the margins
plot(1,1,xlim=c(5,10.7),ylim=c(15,25))
plot(1,1,xlim=c(5,10.7),ylim=c(45,70))
plot(1,1,xlim=c(5,10.7),ylim=c(20,33))
plot(1,1,xlim=c(5,10.7),ylim=c(15,25))
plot(1,1,xlim=c(5,10.7),ylim=c(45,70))
plot(1,1,xlim=c(5,10.7),ylim=c(20,33)) # plot six graphs
labels<-c("aaaaa","bbbbbbbbbbbbbbbbbbbbbbb","ccccccccccc", "ddddd") #legend labels
colour<-c("red","black","dark green","blue")#legend line colours
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 = "n", bty = "n", xaxt = "n", yaxt = "n")## create an empty plot
legend("bottom", lty=1,lwd=1.5,ncol=4,labels,cex=1,
col=colour,bg="white",pt.cex=1,bty="n",text.width=c(0.2,0.2,0.2,0.2)) ## creating legend
【问题讨论】:
-
您提供的代码不会创建绘图。如果问题不容易重现,则很难帮助绘制代码。它可以像
plot(1,1)一样简单,但要确保所有变量都已定义。 -
抱歉代码不清楚。我现在已经编辑了我的问题。