英文:
Plot two counties side by side while preserving scale
问题
有没有一种方法可以将这两个县并排绘制(而不改变实际比例),以比较它们的大小。
我想将圣迭戈和圣克拉拉并排绘制,以展示它们的实际大小。
谢谢。
英文:
Is there a way to plot the two counties side by side (without changing actual scale) to compare their sizes.
I wish to plot San Diego and Santa Clara side by side to demonstrate their actual size.
Thanks
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
答案1
得分: 6
一个直接的方法是通过减去它们的质心将县移动到相同的位置,然后按县名进行分面化。以下是一个完全可复制的示例:
```r
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))
[![enter image description here][1]][1]
<details>
<summary>英文:</summary>
A straightforward way to do this is to move the counties to the same location by subtracting their centroids, then faceting by county name. Here's a fully reproducible example:
```r
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))
答案2
得分: 3
一种解决方案是获取两个对象的质心,并使用coord_sf
将两个图的限制设置为相同的值(即常数)。然后,您可以使用patchwork
将两个图并排放置。
library(tigris)
library(ggplot2)
library(sf)
library(dplyr)
library(patchwork)
# 常数,用于将其与质心相加和相减以设置图的限制
constant <- 1
# 计算两个对象的质心
centroidSD <- san_diego %>%
summarize() %>%
st_centroid() %>%
st_coordinates() %>%
as_tibble()
centroidSC <- santa_clara %>%
summarize() %>%
st_centroid() %>%
st_coordinates() %>%
as_tibble()
# 创建两个图并根据质心和+/-常数设置x和y限制
gg_san_diego <- ggplot() +
geom_sf(data = san_diego,
color="black",
fill="white",
size=0.25) +
coord_sf(xlim = c(centroidSD$X - constant,
centroidSD$X + constant),
ylim = c(centroidSD$Y - constant,
centroidSD$Y + constant))
gg_santa_clara <- ggplot() +
geom_sf(data = santa_clara,
color="black",
fill="white",
size=0.25) +
coord_sf(xlim = c(centroidSC$X - constant,
centroidSC$X + constant),
ylim = c(centroidSC$Y - constant,
centroidSC$Y + constant))
# 使用patchwork将两个图并排放置
gg_san_diego + gg_santa_clara
英文:
One solution is to obtain the centroids of the two objects and set the limits of both plots by the same value (i.e., a constant), using coord_sf
. Then, you can put both plots side by side using patchwork
.
library(tigris)
library(ggplot2)
library(sf)
library(dplyr)
library(patchwork)
# Constant to sum and subtract from centroid
# to set limits of the plots
constant <- 1
# Calculate centroids for both objects
centroidSD <- san_diego |>
summarize() |>
st_centroid() |>
st_coordinates() |>
as_tibble()
centroidSC <- santa_clara |>
summarize() |>
st_centroid()|>
st_coordinates() |>
as_tibble()
# Makes both plots and set x and y limits according to
# centroid and + / - constant
gg_san_diego <- ggplot() +
geom_sf(data = san_diego,
color="black",
fill="white",
size=0.25) +
coord_sf(xlim = c(centroidSD$X - constant,
centroidSD$X + constant),
ylim = c(centroidSD$Y - constant,
centroidSD$Y + constant))
gg_santa_clara <- ggplot() +
geom_sf(data = santa_clara,
color="black",
fill="white",
size=0.25) +
coord_sf(xlim = c(centroidSC$X - constant,
centroidSC$X + constant),
ylim = c(centroidSC$Y - constant,
centroidSC$Y + constant))
# Use patchowrk to put both plots side by side
gg_san_diego + gg_santa_clara
答案3
得分: 1
这是预期的结果。
英文:
is this the expected result
combined_data <- rbind(
data.frame(County = "San Diego", geometry = san_diego$geometry),
data.frame(County = "Santa Clara", geometry = santa_clara$geometry)
)
ggplot(combined_data, aes(geometry = geometry, fill = County)) +
geom_sf(color = "black") +
scale_fill_manual(values = c("blue", "red")) +
labs(title = "San Diego County vs. Santa Clara County", fill = "County") +
theme(legend.position = "right")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论