r - 如何使用 ggplot2 绘制一张美国地图,该地图使用定义的颜色根据 df 中分配的人员填充特定状态?
问题描述
我有一个分配到特定区域的销售人员列表。我想根据分配给该州的销售人员为该州的颜色分配特定的颜色。此外,我希望地图包含州名缩写(即 TX、NY 等)。我也希望能够为特定的人定义颜色。例如#1A1446
用于 Erin,#FFD000
用于 Tim。
library(mapdata)
#> Warning: package 'mapdata' was built under R version 4.1.1
#> Loading required package: maps
#> Warning: package 'maps' was built under R version 4.1.1
library(ggplot2)
library(tidyverse)
#> Warning: package 'tidyr' was built under R version 4.1.1
#> Warning: package 'readr' was built under R version 4.1.1
state <- map_data("state")
assignments <- tibble::tribble(
~State, ~Abbreviation, ~Salesperson,
"alabama", "AL", "Kristen",
"alaska", "AK", "Erin",
"arizona", "AZ", "Erin",
"arkansas", "AR", "Kristen",
"california", "CA", "Erin",
"colorado", "CO", "Erin",
"connecticut", "CT", "Joe",
"delaware", "DE", "Joe",
"florida", "FL", "Tim",
"georgia", "GA", "Tim",
"hawaii", "HI", "Erin",
"idaho", "ID", "Joe",
"illinois", "IL", "Kristen",
"indiana", "IN", "Kristen",
"iowa", "IA", "Kristen",
"kansas", "KS", "Kristen",
"kentucky", "KY", "Kristen",
"louisiana", "LA", "Kristen",
"maine", "ME", "Joe",
"maryland", "MD", "Joe",
"massachusetts", "MA", "Joe",
"michigan", "MI", "Kristen",
"minnesota", "MN", "Kristen",
"mississippi", "MS", "Kristen",
"missouri", "MO", "Kristen",
"montana", "MT", "Erin",
"nebraska", "NE", "Kristen",
"nevada", "NV", "Erin",
"new hampshire", "NH", "Joe",
"new jersey", "NJ", "Joe",
"new mexico", "NM", "Erin",
"new york", "NY", "Joe",
"north carolina", "NC", "Tim",
"north dakota", "ND", "Kristen",
"ohio", "OH", "Kristen",
"oklahoma", "OK", "Kristen",
"oregon", "OR", "Joe",
"pennsylvania", "PA", "Joe",
"rhode island", "RI", "Joe",
"south carolina", "SC", "Tim",
"south dakota", "SD", "Kristen",
"tennessee", "TN", "Kristen",
"texas", "TX", "Kristen",
"utah", "UT", "Joe",
"vermont", "VT", "Joe",
"virginia", "VA", "Tim",
"washington", "WA", "Joe",
"west virginia", "WV", "Kristen",
"wisconsin", "WI", "Kristen",
"wyoming", "WY", "Erin",
"district of columbia", "DC", "Joe"
)
# join salesperson assignments to state df
state_join <- state %>%
left_join(assignments, by = c("region" = "State"))
ggplot(data = state_join, aes(x = long, y = lat, fill = region, group = group)) +
geom_polygon(color = "white") +
guides(fill = FALSE) +
theme(
axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank()
) +
ggtitle("U.S. Map with States") +
coord_fixed(1.3)
#> Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
#> "none")` instead.
解决方案
You need to add the Salesperson as a fill aesthetic in the 'geom_polygon' step.
geom_polygon(aes(fill = Salesperson), color = "black")
The color = "black", makes the state outlines black.
Also, if you want their names to show in the legend, you can remove the 'guides' step.
To add colors, you add another step in the ggplot process:
scale_fill_manual(values = c("#1A1446", "green", "red", "#FFD000"),
breaks = c("Erin", "Joe", "Kristen", "Tim"))
To add the abbreviations, you can make a separate data frame that has the abbreviations and state centers from the datasets state.abbr and state.center included in R.
library(tidyverse)
state_center <- tibble(
Abbreviation = datasets::state.abb,
c_long = datasets::state.center$x,
c_lat = datasets::state.center$y
) %>%
filter(!state.abb %in% c("AK", "HI"))
ggplot() +
geom_polygon(data = state_join,aes(x = long, y = lat, group = group, fill = Salesperson), color = "black") +
geom_text(data = state_center, aes(c_long, c_lat, label = Abbreviation), color = "white") +
theme(
axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(),
axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank()
) +
scale_fill_manual(values = c("#1A1446", "green", "red", "#FFD000"),
breaks = c("Erin", "Joe", "Kristen", "Tim")) +
ggtitle("U.S. Map with States") +
coord_fixed(1.3)
推荐阅读
- node.js - 无法启动 vue 项目。在运行 npm run serve 时显示错误
- python - 这是对 pytest.mark.django_db 的无效使用吗?
- hive - 在 Hive 中创建空视图
- jmeter - 如何获取 2 个 DB 列值并将其与 JMeter 中的单个 JSON 元素进行比较
- html - 修改后如何在我的表单中改回原始值
- django - 我正在尝试获取特定用户的个人帖子。如果我单击特定用户,我想获取他发布的所有帖子 Django
- python - 如何将子文件夹移动到同一个 s3 存储桶 boto3 中的另一个主文件夹?
- azure-devops - 在发布测试计划执行任务中过滤测试用例
- ansible - 如何在 Ansible 中动态使用自定义事实来评估条件语句
- javascript - 如何使用 electron.js 而不是进程列表获取桌面上所有打开的应用程序的列表?