Centring legend below two plots in r

你说的曾经没有我的故事 提交于 2019-12-03 21:16:45

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")

par(xpd=NA) is more what you're looking for. Extract from the ?par help page:

xpd
A logical value or NA. If FALSE, all plotting is clipped to the plot region, if TRUE, all plotting is clipped to the figure region, and if NA, all plotting is clipped to the device region. See also clip.

Indeed, you want it to be clipped to the device region and not the figure region (see, for instance, this blog entry for a graphical explanation of the differences between the plot, the figure and the device regions).

quartz(title="PCoA",12,6)
par(mfrow=c(1,2),oma=c(5,0,0,0),xpd=NA)

plot(1:3,4:6,main="plot 1")

plot(1:3,4:6,main="plot 2")
legend(-0.5,3.5,ncol=3,c("0-1 km","1-5 km","outside barrier"), 
    fill=c("green","orange","red"), title="Fetch")

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!