南方联盟遗迹数据可视化

南方联盟遗迹数据可视化

本文是学习Confederate monuments with the statebins 📦的笔记,讲述了如何使用ggplot2 的geom_count图层和使用statebins包绘制美国方形热力图。

1
2
3
4
5
6
7
8
9
10
11
library(awtools)
confederate <- read_csv('https://raw.githubusercontent.com/awhstin/dataSet-List/master/confederate_symbols.csv')
# 这是一个1484个遗迹名称和位置的数据集
# 整理数据
confederate.na <- subset(
confederate, !is.na(year_dedicated)
)
confederate.na$year_dedicated <- as.numeric(confederate.na$year_dedicated)
confederate.na <- confederate.na %>%
group_by(state) %>%
mutate(counts = length(state))

弗吉尼亚州的遗迹数量:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
va <- subset(confederate.na, state == 'VA')
ggplot(va,
aes(x = year_dedicated,
y = reorder(state, counts),
color = group_class)) +
geom_count(alpha = 0.35,
show.legend = F) +
hrbrthemes::theme_ipsum(
base_family = 'STSongti-SC-Bold'
) +
a_scale_color() +
scale_x_continuous() +
labs(x = '年份',
y = '州') +
theme(axis.title.y = element_text(angle = 0, vjust = 0.5, size = 12))

每个州的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ggplot(confederate.na,
aes(x = year_dedicated,
y = reorder(state, counts),
color = group_class)) +
geom_count(alpha = 0.35,
show.legend = F) +
hrbrthemes::theme_ipsum(
base_family = 'STSongti-SC-Bold'
) +
scale_x_continuous() +
labs(x = '年份',
y = '州',
title = '南方联盟遗迹被投入使用的时间') +
theme(axis.title.y = element_text(angle = 0, vjust = 0.5, size = 12)) +
theme(legend.position = 'top')

statebins绘制美国方形热力图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 各州的地理分布
# statebins包用于绘制美国方形热力图
states <- data.frame(
state = state.abb, stringsAsFactors = F
)
all.mon <- left_join(states, confederate)
all.mon[is.na(all.mon)] <- 0
all.mon %>%
group_by(state) %>%
summarise(count = n()) %>%
select(state = state, value = count) %>%
statebins_continuous(legend_position = 'bottom',
font_size = 3,
text_color = '#ffffff') +
labs(title = '南方联盟遗迹在各州的地理分布', subtitle = paste0('一共', nrow(all.mon), '处')) +
hrbrthemes::theme_ipsum(base_family = 'STSongti-SC-Bold') +
scale_fill_continuous('数量') +
theme(panel.grid = element_blank(),
axis.title = element_blank(),
axis.text = element_blank())

# R

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×