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

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

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

  1. library(tigris)
  2. library(ggplot2)
  3. san_diego <- county_subdivisions(state = "CA", county = "San Diego County", cb = TRUE, year = NULL)
  4. santa_clara <- county_subdivisions(state = "CA", county = "Santa Clara County", cb = TRUE, year = NULL)
  5. gg_san_diego <- ggplot() +
  6. geom_sf(data = san_diego,
  7. color="black",
  8. fill="white",
  9. size=0.25)
  10. gg_santa_clara <- ggplot() +
  11. geom_sf(data = santa_clara,
  12. color="black",
  13. fill="white",
  14. size=0.25)
  15. gg_san_diego
  16. gg_santa_clara

答案1

得分: 6

  1. 一个直接的方法是通过减去它们的质心将县移动到相同的位置,然后按县名进行分面化。以下是一个完全可复制的示例:
  2. ```r
  3. library(tigris)
  4. library(tidyverse)
  5. library(sf)
  6. county_subdivisions(state = "CA", cb = TRUE, year = NULL,
  7. county = c("San Diego County", "Santa Clara County")) %>%
  8. group_by(NAMELSADCO) %>%
  9. mutate(geometry = geometry - st_centroid(st_union(geometry))) %>%
  10. ggplot() +
  11. geom_sf(color = "black", fill = "white") +
  12. facet_wrap(.~NAMELSADCO) +
  13. theme_void(base_size = 20) +
  14. theme(strip.text = element_text(margin = margin(20, 20, 20, 20)),
  15. panel.background = element_rect(fill = "gray95", color = NA))
  1. [![enter image description here][1]][1]
  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. ```r
  5. library(tigris)
  6. library(tidyverse)
  7. library(sf)
  8. county_subdivisions(state = &quot;CA&quot;, cb = TRUE, year = NULL,
  9. county = c(&quot;San Diego County&quot;, &quot;Santa Clara County&quot;)) %&gt;%
  10. group_by(NAMELSADCO) %&gt;%
  11. mutate(geometry = geometry - st_centroid(st_union(geometry))) %&gt;%
  12. ggplot() +
  13. geom_sf(color = &quot;black&quot;, fill = &quot;white&quot;) +
  14. facet_wrap(.~NAMELSADCO) +
  15. theme_void(base_size = 20) +
  16. theme(strip.text = element_text(margin = margin(20, 20, 20, 20)),
  17. panel.background = element_rect(fill = &quot;gray95&quot;, color = NA))

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

答案2

得分: 3

一种解决方案是获取两个对象的质心,并使用coord_sf将两个图的限制设置为相同的值(即常数)。然后,您可以使用patchwork将两个图并排放置。

  1. library(tigris)
  2. library(ggplot2)
  3. library(sf)
  4. library(dplyr)
  5. library(patchwork)
  6. # 常数,用于将其与质心相加和相减以设置图的限制
  7. constant <- 1
  8. # 计算两个对象的质心
  9. centroidSD <- san_diego %>%
  10. summarize() %>%
  11. st_centroid() %>%
  12. st_coordinates() %>%
  13. as_tibble()
  14. centroidSC <- santa_clara %>%
  15. summarize() %>%
  16. st_centroid() %>%
  17. st_coordinates() %>%
  18. as_tibble()
  19. # 创建两个图并根据质心和+/-常数设置x和y限制
  20. gg_san_diego <- ggplot() +
  21. geom_sf(data = san_diego,
  22. color="black",
  23. fill="white",
  24. size=0.25) +
  25. coord_sf(xlim = c(centroidSD$X - constant,
  26. centroidSD$X + constant),
  27. ylim = c(centroidSD$Y - constant,
  28. centroidSD$Y + constant))
  29. gg_santa_clara <- ggplot() +
  30. geom_sf(data = santa_clara,
  31. color="black",
  32. fill="white",
  33. size=0.25) +
  34. coord_sf(xlim = c(centroidSC$X - constant,
  35. centroidSC$X + constant),
  36. ylim = c(centroidSC$Y - constant,
  37. centroidSC$Y + constant))
  38. # 使用patchwork将两个图并排放置
  39. 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.

  1. library(tigris)
  2. library(ggplot2)
  3. library(sf)
  4. library(dplyr)
  5. library(patchwork)
  6. # Constant to sum and subtract from centroid
  7. # to set limits of the plots
  8. constant &lt;- 1
  9. # Calculate centroids for both objects
  10. centroidSD &lt;- san_diego |&gt;
  11. summarize() |&gt;
  12. st_centroid() |&gt;
  13. st_coordinates() |&gt;
  14. as_tibble()
  15. centroidSC &lt;- santa_clara |&gt;
  16. summarize() |&gt;
  17. st_centroid()|&gt;
  18. st_coordinates() |&gt;
  19. as_tibble()
  20. # Makes both plots and set x and y limits according to
  21. # centroid and + / - constant
  22. gg_san_diego &lt;- ggplot() +
  23. geom_sf(data = san_diego,
  24. color=&quot;black&quot;,
  25. fill=&quot;white&quot;,
  26. size=0.25) +
  27. coord_sf(xlim = c(centroidSD$X - constant,
  28. centroidSD$X + constant),
  29. ylim = c(centroidSD$Y - constant,
  30. centroidSD$Y + constant))
  31. gg_santa_clara &lt;- ggplot() +
  32. geom_sf(data = santa_clara,
  33. color=&quot;black&quot;,
  34. fill=&quot;white&quot;,
  35. size=0.25) +
  36. coord_sf(xlim = c(centroidSC$X - constant,
  37. centroidSC$X + constant),
  38. ylim = c(centroidSC$Y - constant,
  39. centroidSC$Y + constant))
  40. # Use patchowrk to put both plots side by side
  41. gg_san_diego + gg_santa_clara

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

答案3

得分: 1

这是预期的结果。

英文:

is this the expected result

  1. combined_data &lt;- rbind(
  2. data.frame(County = &quot;San Diego&quot;, geometry = san_diego$geometry),
  3. data.frame(County = &quot;Santa Clara&quot;, geometry = santa_clara$geometry)
  4. )
  5. ggplot(combined_data, aes(geometry = geometry, fill = County)) +
  6. geom_sf(color = &quot;black&quot;) +
  7. scale_fill_manual(values = c(&quot;blue&quot;, &quot;red&quot;)) +
  8. labs(title = &quot;San Diego County vs. Santa Clara County&quot;, fill = &quot;County&quot;) +
  9. 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:

确定