英文:
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("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") # 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 <- 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) # do this to leave off the color legend
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 = "California Counties",
subtitle = "This is a map of the counties in California with Fresno, San Joaquin, and
Monterrey Counties highlighted."
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("fresno", "san joaquin", "monterey") # note that 'monterey' 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, "highlight", "other")
# create the base plot
ca_base = ggplot(data = ca_df, mapping = aes(x = long, y = lat, group = group)) +
coord_quickmap() +
geom_polygon(color = "black", fill = "gray")
# add the counties, coloring by the highlight variable
ca_base + theme_void() +
geom_polygon(data = ca_county, aes(fill = highlight), color = "white") +
scale_fill_manual(values = c("other" = "gray", "highlight" = "red")) +
geom_polygon(color = "black", fill = NA) +
labs(title = "California Counties",
subtitle = "This is a map of the counties in California with Fresno, San Joaquin, and Monterey Counties highlighted.") +
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)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论