如何在美国地图上用不同颜色阴影或填充县?
How to shade or fill counties with different colors on US map?
我有三个包含一些县 FIP 代码的向量。
在 之后,我能够分别在地图上为每个向量的县添加阴影。
如何在同一张地图上用所有三个矢量遮蔽县?
vec1
应该在 blue
, 中加阴影
vec2
在 red
和
vec3
在 green
.
vec1 <- c(4013, 6037, 17031, 26163, 36059)
vec2 <- c(48045, 1009)
vec3 <- c(48289, 48291)
dt <- countypop %>%
dplyr::mutate(
selected = factor(
ifelse(fips %in% stringr::str_pad(vec1, 5, pad = "0"), "1", "0")
)
)
usmap::plot_usmap(data = dt, values = "selected", color = "grey") +
ggplot2::scale_fill_manual(values = c("blue", "light gray"))
PS: 为什么 par(mfrow=c(3,1))
不给我一个包含三张独特地图的情节?
基本上它是相同的方法,但您可以使用 case_when
来为您的县组分配颜色,而不是使用 ifelse
:
library(ggplot2)
library(usmap)
library(dplyr)
library(stringr)
dt <- countypop %>%
mutate(fill = case_when(
fips %in% str_pad(vec1, 5, pad = "0") ~ "Blue",
fips %in% str_pad(vec2, 5, pad = "0") ~ "Red",
fips %in% str_pad(vec3, 5, pad = "0") ~ "Green",
TRUE ~ "Other"
))
plot_usmap(regions = "counties", data = dt, values = "fill", color = "grey") +
scale_fill_manual(
values = c(Blue = "blue", Green = "green", Red = "red", Other = "light gray")
)
我有三个包含一些县 FIP 代码的向量。
在
如何在同一张地图上用所有三个矢量遮蔽县?
vec1
应该在blue
, 中加阴影
vec2
在red
和vec3
在green
.
vec1 <- c(4013, 6037, 17031, 26163, 36059)
vec2 <- c(48045, 1009)
vec3 <- c(48289, 48291)
dt <- countypop %>%
dplyr::mutate(
selected = factor(
ifelse(fips %in% stringr::str_pad(vec1, 5, pad = "0"), "1", "0")
)
)
usmap::plot_usmap(data = dt, values = "selected", color = "grey") +
ggplot2::scale_fill_manual(values = c("blue", "light gray"))
PS: 为什么 par(mfrow=c(3,1))
不给我一个包含三张独特地图的情节?
基本上它是相同的方法,但您可以使用 case_when
来为您的县组分配颜色,而不是使用 ifelse
:
library(ggplot2)
library(usmap)
library(dplyr)
library(stringr)
dt <- countypop %>%
mutate(fill = case_when(
fips %in% str_pad(vec1, 5, pad = "0") ~ "Blue",
fips %in% str_pad(vec2, 5, pad = "0") ~ "Red",
fips %in% str_pad(vec3, 5, pad = "0") ~ "Green",
TRUE ~ "Other"
))
plot_usmap(regions = "counties", data = dt, values = "fill", color = "grey") +
scale_fill_manual(
values = c(Blue = "blue", Green = "green", Red = "red", Other = "light gray")
)