Centring legend below two plots in r

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

UPDATE

2 个回复

``````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)`更符合您的要求。 `?par`帮助页面中提取：

`xpd`

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

1 在 R 图的图例中叠加两个圆圈

3 在 R 中组合两个图例 [重复]

2021-01-06 15:33:48 1 48   r/ ggplot2
4 如何在 GeoPandas 的图例中显示两个颜色图

5 合并两个图，以便系列的颜色在图例中重合

2016-10-21 12:50:32 1 45   r/ ggplot2
7 ggplot合并图例中两个重叠图的形状

2017-09-01 08:50:03 1 242   plot/ aes
8 如何在R中具有两个图层的图上添加图例

2013-06-28 17:14:59 1 963   r/ legend
9 如何抑制/组合 R 中两个几何图形的图例

2021-03-21 18:47:07 1 16   r/ ggplot2
10 我可以在 R 中的 ggplot 中分离两个填充图例吗？

2019-12-05 22:21:56 1 141   r/ ggplot2