使图例在r中的两个图下面居中 [英] Centring legend below two plots in r
问题描述
我想将一个常见的图例放在两个图的下方.我使用xpd=TRUE
允许在图本身之外进行打印,并使用oma为图例创建空间.但是,图例不会沿水平方向移动,而会在垂直方向早"被修剪.有什么建议吗?
I would like to centre a common legend below two plots. I have used xpd=TRUE
to allow for printing outside the plot itself and oma to create space for the legend. However the legend will not move horizonatally and gets clipped 'early' vertically. Any advice?
quartz(title="PCoA",12,6)
par(mfrow=c(1,2),oma=c(5,0,0,0),xpd=TRUE)
plot(1:3,4:6,main="plot 1")
plot(1:3,4:6,main="plot 2")
# Clips the plot
legend(1,3.5,ncol=3,c("0-1 km","1-5 km","outside barrier"),fill=c("green","orange","red"), title="Fetch")
# Won't let me move the legend sideways
legend(0,3.5,ncol=3,c("0-1 km","1-5 km","outside barrier"),fill=c("green","orange","red"), title="Fetch")
更新
使用下面的解决方案,它会切割图形的边缘,通过拖动边缘来更改图形的尺寸,该边缘将变得可见(请参见下文).有任何想法可能会发生什么事吗?
With the solution below it cuts of the edge of the graph which becomes visible by changing the dimension of the figure by dragging the edge (see below). Any ideas what might be going on?
推荐答案
而不是使用par=mfrow(...)
,我建议您使用layout()
.
Rather than using par=mfrow(...)
I suggest you use layout()
.
这允许您指定具有绘图位置的矩阵:
This allows you to specify a matrix with plot positions:
layout(matrix(c(1,2,3,3), ncol=2, byrow=TRUE), heights=c(4, 1))
par(mai=rep(0.5, 4))
plot(1:3,4:6,main="plot 1")
plot(1:3,4:6,main="plot 2")
par(mai=c(0,0,0,0))
plot.new()
legend(x="center", ncol=3,legend=c("0-1 km","1-5 km","outside barrier"),
fill=c("green","orange","red"), title="Fetch")
这篇关于使图例在r中的两个图下面居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!