Zhiqiang Wang
Zhiqiang Wang

Reputation: 6759

Change the size on only part of a map in R

Here is a reproducible example. I have obtained the US and UK maps. My question is how to double the size of UK part without changing the size of US part on the same plot. Thanks.

library(tidyverse)
library(rnaturalearth)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(ggthemes)
map_uk<- st_as_sf(ne_countries(country = 'united kingdom' , 
                                            type = 'map_units')) %>% 
  select(country = admin, name, geometry)

map_us <- st_as_sf(ne_states(country = "united states of america"
                                         )) %>% 
  select(country = admin, name, geometry) %>% 
  filter(!name %in% c("Alaska", "Hawaii"))
map_both <- map_uk %>% rbind(map_us)
map_both %>% 
  ggplot()+
  geom_sf(aes(geometry = geometry, fill = name)) + 
  theme_map() +
  theme(legend.position = "none")

Created on 2021-04-19 by the reprex package (v2.0.0)

Upvotes: 0

Views: 257

Answers (1)

A.Chrlt
A.Chrlt

Reputation: 316

Maybe with cowplot :

Here is my attempt :

library(tidyverse)
library(rnaturalearth)
library(sf)
library(ggthemes)
library(cowplot)

map_uk<- st_as_sf(ne_countries(country = 'united kingdom' , 
                               type = 'map_units')) %>% 
  select(country = admin, name, geometry)

map_us <- st_as_sf(ne_states(country = "united states of america")) %>% 
  select(country = admin, name, geometry) %>%
  filter(!name %in% c("Alaska", "Hawaii"))


US <- ggplot(data=map_us) + 
  geom_sf() +
  theme_map()

UK <- ggplot(data=map_uk) + 
  geom_sf() +
  theme_map()



ggdraw() + draw_plot(US, x=0,y=0,width = 0.75, height = 0.8) + draw_plot(UK, x=0.65,y=0.4,width = 0.45, height = 0.4)

enter image description here

Second attempt

map_both <- map_uk %>% rbind(map_us)

both <- map_both %>%
  ggplot()+
  geom_sf(aes(geometry = geometry, fill = name)) + 
  theme_map() +
  theme(legend.position = "none")


UK2 <- both %+% subset(map_both,country %in% c("United Kingdom"))
US2 <- both %+% subset(map_both,country %in% c("United States of America"))

plot_grid(US2,UK2,ncol=2, scale=c(1,0.4))

enter image description here

Upvotes: 1

Related Questions