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

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

How do can I color in specific counties in R?

问题

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

install.packages(c("maps", "mapdata"))

library(tidyverse)
library(mapdata)

library(maps)
library(stringr)
library(viridis)

usa <- map_data("usa")

dim(usa)

head(usa)


tail(usa)

usa <- map_data("usa") # 我们已经做过这个了,但我们可以再做一次
ggplot() + 
geom_polygon(data = usa, aes(x = long, y = lat, group = group)) + 
coord_quickmap()


states <- map_data("state")
dim(states)

tail(states)

ggplot(data = states) + 
geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = "white") + 
coord_quickmap() +
guides(fill = FALSE)  # 这样做是为了不显示颜色图例

ca_df <- states %>%
  filter(region == "california")

head(ca_df)

counties <- map_data("county") 
ca_county <- counties %>%
  filter(region == "california")

head(ca_county)

ca_base <- ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) + 
coord_quickmap() + 
geom_polygon(color = "black", fill = "gray")
ca_base + theme_void()

ca_base + theme_void() + 
geom_polygon(data = ca_county, fill = NA , color = "white") + 
geom_polygon(color = "black", fill = NA)   + 
labs(title = "加利福尼亚县",
   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

install.packages(c(&quot;maps&quot;, &quot;mapdata&quot;))

library(tidyverse)
library(mapdata)

library(maps)
library(stringr)
 library(viridis)
    
 usa &lt;- map_data(&quot;usa&quot;)

 dim(usa)

 head(usa)


 tail(usa)

 usa &lt;- map_data(&quot;usa&quot;) # we already did this, but we can do it again
 ggplot() + 
 geom_polygon(data = usa, aes(x = long, y = lat, group = group)) + 
 coord_quickmap()


 states &lt;- map_data(&quot;state&quot;)
 dim(states)

 tail(states)

 ggplot(data = states) + 
 geom_polygon(aes(x = long, y = lat, fill = region, group = group), color = &quot;white&quot;) + 
 coord_quickmap() +
 guides(fill = FALSE)  # do this to leave off the color legend

ca_df &lt;- states %&gt;%
  filter(region == &quot;california&quot;)

head(ca_df)

counties &lt;- map_data(&quot;county&quot;) ca_county &lt;- counties %&gt;% filter(region == &quot;california&quot;)

head(ca_county)

ca_base &lt;- ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) + 
 coord_quickmap() + 
  geom_polygon(color = &quot;black&quot;, fill = &quot;gray&quot;)
 ca_base + theme_void()

ca_base + theme_void() + 
 geom_polygon(data = ca_county, fill = NA , color = &quot;white&quot;) + 
 geom_polygon(color = &quot;black&quot;, fill = NA)   + 
 labs(title = &quot;California Counties&quot;,
   subtitle = &quot;This is a map of the counties in California with Fresno, San Joaquin, and           
    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.

# define your three target counties
target_counties = c(&quot;fresno&quot;, &quot;san joaquin&quot;, &quot;monterey&quot;)  # note that &#39;monterey&#39; is the correct spelling

# add a new column to identify whether each county is a target county
ca_county$highlight = if_else(ca_county$subregion %in% target_counties, &quot;highlight&quot;, &quot;other&quot;)

# create the base plot
ca_base = ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) + 
  coord_quickmap() + 
  geom_polygon(color = &quot;black&quot;, fill = &quot;gray&quot;)

# add the counties, coloring by the highlight variable
ca_base + theme_void() + 
  geom_polygon(data = ca_county, aes(fill = highlight), color = &quot;white&quot;) +
  scale_fill_manual(values = c(&quot;other&quot; = &quot;gray&quot;, &quot;highlight&quot; = &quot;red&quot;)) +
  geom_polygon(color = &quot;black&quot;, fill = NA)   + 
  labs(title = &quot;California Counties&quot;,
       subtitle = &quot;This is a map of the counties in California with Fresno, San Joaquin, and Monterey Counties highlighted.&quot;) + 
  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:

确定