使用coord_equal()对ggplot地图图层进行对齐

4

我在对齐两个地图时遇到了困难。以下是使用可重复示例:

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)) 然后再次使用 plot_grid 添加 legend legend <- get_legend(pl1)。 当我使用coord_equal时,也会出现大量空白,但我无法去除它(我不保存图形,只是显示它)。

enter image description here

1个回答

4

我更喜欢使用patchwork软件包对图形进行对齐,它是一个简单的框架(带有许多附加功能),并且开箱即用可以满足您的要求。

library(patchwork)
pl2 + pl1

enter image description here


就世界地图上的空白区域而言,我认为这是一个图形设备问题 - caldwellst
@caldwest,没错!我将宽高比设置为1:4,白色空间消失了。 - yuliaUU

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接