我可以帮你翻译这句话:如何在R中着色特定的县?

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

How do can I color in specific counties in R?

问题

我想要创建一个加利福尼亚州的地图,其中弗雷斯诺、圣华金和蒙特雷县使用R着色。我已经创建了一个加利福尼亚州的地图,包括各个县,但是我无法对特定的县进行着色。以下是我的R代码:

  1. install.packages(c("maps", "mapdata"))
  2. library(tidyverse)
  3. library(mapdata)
  4. library(maps)
  5. library(stringr)
  6. library(viridis)
  7. usa <- map_data("usa")
  8. dim(usa)
  9. head(usa)
  10. tail(usa)
  11. usa <- map_data("usa") # 我们已经做过这个了,但我们可以再做一次
  12. ggplot() +
  13. geom_polygon(data = usa, aes(x = long, y = lat, group = group)) +
  14. coord_quickmap()
  15. states <- map_data("state")
  16. dim(states)
  17. tail(states)
  18. ggplot(data = states) +
  19. geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = "white") +
  20. coord_quickmap() +
  21. guides(fill = FALSE) # 这样做是为了不显示颜色图例
  22. ca_df <- states %>%
  23. filter(region == "california")
  24. head(ca_df)
  25. counties <- map_data("county")
  26. ca_county <- counties %>%
  27. filter(region == "california")
  28. head(ca_county)
  29. ca_base <- ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) +
  30. coord_quickmap() +
  31. geom_polygon(color = "black", fill = "gray")
  32. ca_base + theme_void()
  33. ca_base + theme_void() +
  34. geom_polygon(data = ca_county, fill = NA , color = "white") +
  35. geom_polygon(color = "black", fill = NA) +
  36. labs(title = "加利福尼亚县",
  37. subtitle = "这是加利福尼亚县的地图,其中包括弗雷斯诺、圣华金和蒙特雷县。")

我尝试更改了"fill"选项,但是这会更改整个地区的颜色。ca_county$subregion包含了县的信息。

英文:

I would like to create a map of California with Fresno, San Joaquin and Monterrey county colored in using R. I have created a map of California with counties however I am unable to color in the specific counties. Below is my R code

  1. install.packages(c(&quot;maps&quot;, &quot;mapdata&quot;))
  2. library(tidyverse)
  3. library(mapdata)
  4. library(maps)
  5. library(stringr)
  6. library(viridis)
  7. usa &lt;- map_data(&quot;usa&quot;)
  8. dim(usa)
  9. head(usa)
  10. tail(usa)
  11. usa &lt;- map_data(&quot;usa&quot;) # we already did this, but we can do it again
  12. ggplot() +
  13. geom_polygon(data = usa, aes(x = long, y = lat, group = group)) +
  14. coord_quickmap()
  15. states &lt;- map_data(&quot;state&quot;)
  16. dim(states)
  17. tail(states)
  18. ggplot(data = states) +
  19. geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = &quot;white&quot;) +
  20. coord_quickmap() +
  21. guides(fill = FALSE) # do this to leave off the color legend
  22. ca_df &lt;- states %&gt;%
  23. filter(region == &quot;california&quot;)
  24. head(ca_df)
  25. counties &lt;- map_data(&quot;county&quot;) ca_county &lt;- counties %&gt;% filter(region == &quot;california&quot;)
  26. head(ca_county)
  27. ca_base &lt;- ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) +
  28. coord_quickmap() +
  29. geom_polygon(color = &quot;black&quot;, fill = &quot;gray&quot;)
  30. ca_base + theme_void()
  31. ca_base + theme_void() +
  32. geom_polygon(data = ca_county, fill = NA , color = &quot;white&quot;) +
  33. geom_polygon(color = &quot;black&quot;, fill = NA) +
  34. labs(title = &quot;California Counties&quot;,
  35. subtitle = &quot;This is a map of the counties in California with Fresno, San Joaquin, and
  36. Monterrey Counties highlighted.&quot;

I tried changing the "fill" option howeves this changed the color of the whole region. ca_county$subregion containts the county information.

答案1

得分: 1

这个脚本创建了一个名为“highlight”的新变量,对于你的三个目标县,它的值是“highlight”,对于所有其他县,它的值是“other”。然后,这个变量被用在geom_polygon函数的aes参数中以填充不同的颜色。scale_fill_manual函数用于设置颜色,你的目标县被设为红色,所有其他县被设为灰色。

P.S. 我去掉了着色方案,并在副标题中描述了它。如果你有其他偏好,可以移除guides(fill = FALSE)

英文:

You were almost there! First you need to set up a coloring variable in your ca_county dataframe, and then use that variable as the fill parameter in the geom_polygon function.

  1. # define your three target counties
  2. target_counties = c(&quot;fresno&quot;, &quot;san joaquin&quot;, &quot;monterey&quot;) # note that &#39;monterey&#39; is the correct spelling
  3. # add a new column to identify whether each county is a target county
  4. ca_county$highlight = if_else(ca_county$subregion %in% target_counties, &quot;highlight&quot;, &quot;other&quot;)
  5. # create the base plot
  6. ca_base = ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) +
  7. coord_quickmap() +
  8. geom_polygon(color = &quot;black&quot;, fill = &quot;gray&quot;)
  9. # add the counties, coloring by the highlight variable
  10. ca_base + theme_void() +
  11. geom_polygon(data = ca_county, aes(fill = highlight), color = &quot;white&quot;) +
  12. scale_fill_manual(values = c(&quot;other&quot; = &quot;gray&quot;, &quot;highlight&quot; = &quot;red&quot;)) +
  13. geom_polygon(color = &quot;black&quot;, fill = NA) +
  14. labs(title = &quot;California Counties&quot;,
  15. subtitle = &quot;This is a map of the counties in California with Fresno, San Joaquin, and Monterey Counties highlighted.&quot;) +
  16. guides(fill = FALSE)

This script creates a new variable highlight that has the value "highlight" for your three target counties and "other" for all other counties. This variable is then used in the aes argument of geom_polygon to fill the counties with different colors. The scale_fill_manual function is used to set the colors, with your target counties in red and all other counties in gray.

P.S. I took the liberty to remove the coloring scheme, describing it in the subtitle. If you prefer otherwise, remove guides(fill = FALSE).

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

发表评论

匿名网友

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

确定