将ggplot地图图与coord_equal()对齐

问题描述 投票:4回答:1

我在对齐两张地图时遇到麻烦。我在下面使用可复制的示例:

library(ggplot2)
library(cowplot)
world <- map_data("world")

pl2= ggplot() +
  geom_polygon(data=world, aes(x=long, y=lat, group=group)) +
  theme_bw()+
  coord_equal()
pl1 <- ggplot() +
  geom_polygon(data=world, aes(x=long, y=lat, group=group, color=group)) +
  coord_equal()
plot_grid(pl2, pl1 + theme(legend.justification = c(0,1)), align="h",axis = "bt")

我尝试了各种方法,例如设置图形的宽度和高度,尝试了scale=align="h",axis = "bt"中的各种选项我也尝试过plot_grid(pl2, pl1+ theme(legend.position = "none"), align="h", scale=c(1,1)),然后再次将legend legend <- get_legend(pl1)plot_grid相加。当我使用coord_equal时,我还有大量的空白,这是我无法摆脱的(我不保存图形,只是显示它)

enter image description here

r dictionary ggplot2 cowplot
1个回答
3
投票

[我更喜欢使用patchwork包来对齐图,简单的框架(具有许多附加功能),并且开箱即用即可满足您的需要。

library(patchwork)
pl2 + pl1

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.