4

I am having trouble aligning two maps. I am using a reproducible example below:

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

I tried various things like setting the figure widths and heights, trying scale=,various options from align="h",axis = "bt" I also tried plot_grid(pl2, pl1+ theme(legend.position = "none"), align="h", scale=c(1,1)) and then add legend legend <- get_legend(pl1) with plot_grid again. I also have a crazy amount of white space when I use coord_equal, that I can't get rid of ( I am not saving the graph, just displaying it)

enter image description here

yuliaUU
  • 1,581
  • 2
  • 12
  • 33

1 Answers1

4

I prefer using the patchwork package for aligning plots, simple framework (with lots of additional functionality), and out of the box does what you want here.

library(patchwork)
pl2 + pl1

enter image description here

caldwellst
  • 5,719
  • 6
  • 22
  • In terms of white space on the world map, I believe it's a [graphics device issue](https://stackoverflow.com/questions/13343726/how-to-reduce-white-space-margins-of-world-map). – caldwellst Apr 03 '20 at 07:37
  • @caldwest, yep, indeed! I set the ratio width:length 1:4 and white space dissapear – yuliaUU Apr 03 '20 at 08:04