首页 > 解决方案 > 在 R 中仅更改部分地图的大小

问题描述

这是一个可重现的例子。我已经获得了美国和英国的地图。我的问题是如何在不改变同一地块上美国部分的大小的情况下将英国部分的大小增加一倍。谢谢。

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

reprex 包于 2021-04-19 创建 (v2.0.0 )

标签: rggplot2sf

解决方案


也许与cowplot

这是我的尝试:

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)

在此处输入图像描述

第二次尝试

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

在此处输入图像描述


推荐阅读