美国人口普查部门在 ggplot2 中的映射
Mapping by US Census Divisions in ggplot2
我想按美国人口普查区域映射数据。简而言之,我有州级数据,我使用 maps 包将其加入 latitude/longitude 坐标。这是结果数据帧的前 20 行:
df_F4 %>% select(state, division, percap_rheum, group, long, lat) %>% print(n=20)
# A tibble: 15,539 x 6
# Groups: division [9]
state division percap_rheum group long lat
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 alabama east south central 70255. 1 -87.5 30.4
2 alabama east south central 70255. 1 -87.5 30.4
3 alabama east south central 70255. 1 -87.5 30.4
4 alabama east south central 70255. 1 -87.5 30.3
5 alabama east south central 70255. 1 -87.6 30.3
6 alabama east south central 70255. 1 -87.6 30.3
7 alabama east south central 70255. 1 -87.6 30.3
8 alabama east south central 70255. 1 -87.6 30.3
9 alabama east south central 70255. 1 -87.7 30.3
10 alabama east south central 70255. 1 -87.8 30.3
11 alabama east south central 70255. 1 -87.9 30.2
12 alabama east south central 70255. 1 -87.9 30.2
13 alabama east south central 70255. 1 -88.0 30.2
14 alabama east south central 70255. 1 -88.0 30.2
15 alabama east south central 70255. 1 -88.0 30.3
16 alabama east south central 70255. 1 -88.0 30.3
17 alabama east south central 70255. 1 -88.0 30.3
18 alabama east south central 70255. 1 -88.0 30.3
19 alabama east south central 70255. 1 -87.9 30.3
20 alabama east south central 70255. 1 -87.8 30.3
# … with 15,519 more rows
我想用 ggplot2 绘制它,按人口普查区域组织。我按地区汇总了数据,并可以在州一级绘制图表:
graph_theme <- theme_light() +
theme(
text = element_text(size=10),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = unit(c(0.75, 0.25, 0.5, 0.5), "cm")) #top, R, bottom, L)
map_theme <- theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
legend.title = element_blank(),
legend.position = c(0.92, 0.25), # h / v
legend.background = element_blank()
)
df_F4 %>%
ggplot(aes(
long,
lat,
group = group)) +
geom_polygon(aes(fill = percap_rheum), color = "white") +
scale_fill_viridis_c(labels = dollar_format(big.mark = ","), direction = -1) +
graph_theme +
map_theme
结果是这张图片:
你可以看出那里有人口普查部门,但我想突出显示或以某种方式概述它们。任何建议将不胜感激!
一种方法是为每个分区创建一个多边形,然后将它们覆盖在州数据上。
使用 tigris::state()
中的数据的示例。步骤是:
- 下载数据并过滤到美国大陆,
- 创建分区的多边形,
- 绘制覆盖分区边界的状态数据。
我还更改为地理 crs,使美国看起来有点弯曲。不过不必这样做。
library(tidyverse)
library(tigris)
library(sf)
# Download state data and filter states
sts <- states() %>%
filter(!STUSPS %in% c('HI', 'AK', 'PR', 'GU', 'VI', 'AS', 'MP'))
# Summarize to DIVISION polygons, see sf::st_union
div <- sts %>%
group_by(DIVISION) %>%
summarize()
# Plot it
ggplot() +
theme_void() +
geom_sf(data = sts,
aes(fill = as.numeric(DIVISION)),
color = 'white') +
geom_sf(data = div,
color = 'black',
fill = NA,
size = 1) +
scale_fill_viridis_c() +
coord_sf(crs = 5070) +
labs(fill = NULL)
编辑:更新为使用 maps
包。找到有用的提示
# get data specifying which states are in which division
div_dat <- states(cb = FALSE, resolution = '20m') %>%
st_drop_geometry() %>%
select(NAME, DIVISION) %>%
mutate(ID = tolower(NAME))
# get state data, convert to sf, join with division data
states <- maps::map("state", plot = FALSE, fill = TRUE) %>%
st_as_sf() %>%
left_join(div_dat)
# create division polygons
div <- states %>%
group_by(DIVISION) %>%
summarize()
# plot it
ggplot() +
theme_void() +
geom_sf(data = states,
aes(fill = as.numeric(DIVISION)),
color = 'white') +
geom_sf(data = div,
color = 'black',
fill = NA,
size = 1) +
scale_fill_viridis_c() +
coord_sf(crs = 5070) +
labs(fill = NULL)
信任上面的@nniloc 以获得对此的答案。如果其他人跟进,我们使用 geom_sf 解决了一些分辨率问题。我尝试了一段时间让 geom_polygon 和“地图”包数据工作。它只是更高的分辨率,因为我认为有更多的数据点。
总之,我终于妥协了。无法弯曲地图,但州界线本身看起来不错。简而言之,我在之前创建的地图上使用了上面描述的分区叠加层。结果看起来不错,我想如果它对其他人有帮助我会分享。
# get data specifying which states are in which division
div_dat <- states(cb = FALSE, resolution = '20m') %>%
st_drop_geometry() %>%
select(NAME, DIVISION) %>%
mutate(ID = tolower(NAME))
# get state data, convert to sf, join with division data
states <- maps::map("state", plot = FALSE, fill = TRUE) %>%
st_as_sf() %>%
left_join(div_dat)
# create division polygons
div <- states %>%
group_by(DIVISION) %>%
summarize()
# Plot percapita spending
ggplot() +
graph_theme +
map_theme +
geom_polygon(data = df_F4,
aes(long, lat, group = group, fill = percap_rheum),
color = "white") +
geom_sf(data = div,
color = "#838383",
fill = NA,
size = 1) +
scale_fill_viridis_c(labels = dollar_format(big.mark = ","), direction = -1)
我想按美国人口普查区域映射数据。简而言之,我有州级数据,我使用 maps 包将其加入 latitude/longitude 坐标。这是结果数据帧的前 20 行:
df_F4 %>% select(state, division, percap_rheum, group, long, lat) %>% print(n=20)
# A tibble: 15,539 x 6
# Groups: division [9]
state division percap_rheum group long lat
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 alabama east south central 70255. 1 -87.5 30.4
2 alabama east south central 70255. 1 -87.5 30.4
3 alabama east south central 70255. 1 -87.5 30.4
4 alabama east south central 70255. 1 -87.5 30.3
5 alabama east south central 70255. 1 -87.6 30.3
6 alabama east south central 70255. 1 -87.6 30.3
7 alabama east south central 70255. 1 -87.6 30.3
8 alabama east south central 70255. 1 -87.6 30.3
9 alabama east south central 70255. 1 -87.7 30.3
10 alabama east south central 70255. 1 -87.8 30.3
11 alabama east south central 70255. 1 -87.9 30.2
12 alabama east south central 70255. 1 -87.9 30.2
13 alabama east south central 70255. 1 -88.0 30.2
14 alabama east south central 70255. 1 -88.0 30.2
15 alabama east south central 70255. 1 -88.0 30.3
16 alabama east south central 70255. 1 -88.0 30.3
17 alabama east south central 70255. 1 -88.0 30.3
18 alabama east south central 70255. 1 -88.0 30.3
19 alabama east south central 70255. 1 -87.9 30.3
20 alabama east south central 70255. 1 -87.8 30.3
# … with 15,519 more rows
我想用 ggplot2 绘制它,按人口普查区域组织。我按地区汇总了数据,并可以在州一级绘制图表:
graph_theme <- theme_light() +
theme(
text = element_text(size=10),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
plot.margin = unit(c(0.75, 0.25, 0.5, 0.5), "cm")) #top, R, bottom, L)
map_theme <- theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
legend.title = element_blank(),
legend.position = c(0.92, 0.25), # h / v
legend.background = element_blank()
)
df_F4 %>%
ggplot(aes(
long,
lat,
group = group)) +
geom_polygon(aes(fill = percap_rheum), color = "white") +
scale_fill_viridis_c(labels = dollar_format(big.mark = ","), direction = -1) +
graph_theme +
map_theme
结果是这张图片:
你可以看出那里有人口普查部门,但我想突出显示或以某种方式概述它们。任何建议将不胜感激!
一种方法是为每个分区创建一个多边形,然后将它们覆盖在州数据上。
使用 tigris::state()
中的数据的示例。步骤是:
- 下载数据并过滤到美国大陆,
- 创建分区的多边形,
- 绘制覆盖分区边界的状态数据。
我还更改为地理 crs,使美国看起来有点弯曲。不过不必这样做。
library(tidyverse)
library(tigris)
library(sf)
# Download state data and filter states
sts <- states() %>%
filter(!STUSPS %in% c('HI', 'AK', 'PR', 'GU', 'VI', 'AS', 'MP'))
# Summarize to DIVISION polygons, see sf::st_union
div <- sts %>%
group_by(DIVISION) %>%
summarize()
# Plot it
ggplot() +
theme_void() +
geom_sf(data = sts,
aes(fill = as.numeric(DIVISION)),
color = 'white') +
geom_sf(data = div,
color = 'black',
fill = NA,
size = 1) +
scale_fill_viridis_c() +
coord_sf(crs = 5070) +
labs(fill = NULL)
编辑:更新为使用 maps
包。找到有用的提示
# get data specifying which states are in which division
div_dat <- states(cb = FALSE, resolution = '20m') %>%
st_drop_geometry() %>%
select(NAME, DIVISION) %>%
mutate(ID = tolower(NAME))
# get state data, convert to sf, join with division data
states <- maps::map("state", plot = FALSE, fill = TRUE) %>%
st_as_sf() %>%
left_join(div_dat)
# create division polygons
div <- states %>%
group_by(DIVISION) %>%
summarize()
# plot it
ggplot() +
theme_void() +
geom_sf(data = states,
aes(fill = as.numeric(DIVISION)),
color = 'white') +
geom_sf(data = div,
color = 'black',
fill = NA,
size = 1) +
scale_fill_viridis_c() +
coord_sf(crs = 5070) +
labs(fill = NULL)
信任上面的@nniloc 以获得对此的答案。如果其他人跟进,我们使用 geom_sf 解决了一些分辨率问题。我尝试了一段时间让 geom_polygon 和“地图”包数据工作。它只是更高的分辨率,因为我认为有更多的数据点。
总之,我终于妥协了。无法弯曲地图,但州界线本身看起来不错。简而言之,我在之前创建的地图上使用了上面描述的分区叠加层。结果看起来不错,我想如果它对其他人有帮助我会分享。
# get data specifying which states are in which division
div_dat <- states(cb = FALSE, resolution = '20m') %>%
st_drop_geometry() %>%
select(NAME, DIVISION) %>%
mutate(ID = tolower(NAME))
# get state data, convert to sf, join with division data
states <- maps::map("state", plot = FALSE, fill = TRUE) %>%
st_as_sf() %>%
left_join(div_dat)
# create division polygons
div <- states %>%
group_by(DIVISION) %>%
summarize()
# Plot percapita spending
ggplot() +
graph_theme +
map_theme +
geom_polygon(data = df_F4,
aes(long, lat, group = group, fill = percap_rheum),
color = "white") +
geom_sf(data = div,
color = "#838383",
fill = NA,
size = 1) +
scale_fill_viridis_c(labels = dollar_format(big.mark = ","), direction = -1)