SiD*_*SiD 1 r ggplot2 tigris tmap r-sf
有没有办法并排绘制两个县(不改变实际比例)来比较它们的大小。
我希望并排绘制圣地亚哥和圣克拉拉,以展示它们的实际大小。
谢谢
library(tigris)
library(ggplot2)
san_diego <- county_subdivisions(state = "CA", county = "San Diego County", cb = TRUE, year = NULL)
santa_clara <- county_subdivisions(state = "CA", county = "Santa Clara County", cb = TRUE, year = NULL)
gg_san_diego <- ggplot() +
geom_sf(data = san_diego,
color="black",
fill="white",
size=0.25)
gg_santa_clara <- ggplot() +
geom_sf(data = santa_clara,
color="black",
fill="white",
size=0.25)
gg_san_diego
gg_santa_clara
Run Code Online (Sandbox Code Playgroud)
一种简单的方法是通过减去县的质心,然后按县名称进行分面,将县移动到同一位置。这是一个完全可重现的示例:
library(tigris)
library(tidyverse)
library(sf)
county_subdivisions(state = "CA", cb = TRUE, year = NULL,
county = c("San Diego County", "Santa Clara County")) %>%
group_by(NAMELSADCO) %>%
mutate(geometry = geometry - st_centroid(st_union(geometry))) %>%
ggplot() +
geom_sf(color = "black", fill = "white") +
facet_wrap(.~NAMELSADCO) +
theme_void(base_size = 20) +
theme(strip.text = element_text(margin = margin(20, 20, 20, 20)),
panel.background = element_rect(fill = "gray95", color = NA))
Run Code Online (Sandbox Code Playgroud)