在保持比例的情况下并排绘制两个县。

huangapple go评论161阅读模式
英文:

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&#39;s a fully reproducible example:
```r
library(tigris)
library(tidyverse)
library(sf)

county_subdivisions(state = &quot;CA&quot;, cb = TRUE, year = NULL,
                    county = c(&quot;San Diego County&quot;, &quot;Santa Clara County&quot;)) %&gt;%
  group_by(NAMELSADCO) %&gt;%
  mutate(geometry = geometry - st_centroid(st_union(geometry))) %&gt;%
  ggplot() + 
  geom_sf(color = &quot;black&quot;, fill = &quot;white&quot;) +
  facet_wrap(.~NAMELSADCO) +
  theme_void(base_size = 20) +
  theme(strip.text = element_text(margin = margin(20, 20, 20, 20)),
        panel.background = element_rect(fill = &quot;gray95&quot;, 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 &lt;- 1

# Calculate centroids for both objects
centroidSD &lt;- san_diego |&gt;
  summarize() |&gt;
  st_centroid() |&gt;
  st_coordinates() |&gt;
  as_tibble()

centroidSC &lt;- santa_clara |&gt;
  summarize() |&gt;
  st_centroid()|&gt;
  st_coordinates() |&gt;
  as_tibble()

# Makes both plots and set x and y limits according to 
# centroid and + / - constant
gg_san_diego &lt;- ggplot() + 
  geom_sf(data = san_diego, 
          color=&quot;black&quot;,
          fill=&quot;white&quot;, 
          size=0.25) + 
  coord_sf(xlim = c(centroidSD$X - constant,
                    centroidSD$X + constant),
           ylim = c(centroidSD$Y - constant,
                    centroidSD$Y + constant))

gg_santa_clara &lt;- ggplot() + 
  geom_sf(data = santa_clara, 
          color=&quot;black&quot;,
          fill=&quot;white&quot;, 
          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 &lt;- rbind(
  data.frame(County = &quot;San Diego&quot;, geometry = san_diego$geometry),
  data.frame(County = &quot;Santa Clara&quot;, geometry = santa_clara$geometry)
)

ggplot(combined_data, aes(geometry = geometry, fill = County)) +
  geom_sf(color = &quot;black&quot;) +
  scale_fill_manual(values = c(&quot;blue&quot;, &quot;red&quot;)) +
  labs(title = &quot;San Diego County vs. Santa Clara County&quot;, fill = &quot;County&quot;) +
  theme(legend.position = &quot;right&quot;)

在保持比例的情况下并排绘制两个县。

huangapple
  • 本文由 发表于 2023年8月11日 05:13:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76879336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定