rm(list=ls())
load(file = "/Users/n.w./Documents/countypres_2000-2016.rdata")
library(tidyverse)
library(here)
library(janitor)
library(albersusa)
library(sf)
library(sp)
library(rgeos)
library(maptools)
library(ggthemes)
library(viridis)
library(scales)
library(glue)
library(jkmisc)
Package不是每个都用上的,准确说是大多数都没用的,习惯性全加上了
Data 是MIT election的,数据库为2000-2016年美国总统大选,以country为最小单位,具体link
https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/VOQCHQ
这里只选取2016年的做例子
data_2016_remove <- x %>% filter(year == 2016, party == 'NA') %>%
mutate(totalvotes = totalvotes - candidatevotes)
data_2016 <- x %>% filter(year == 2016, party == "democrat") %>%
mutate(totalvotes = data_2016_remove$totalvotes) %>%
mutate(percen = candidatevotes/totalvotes,
id = ifelse(str_length(as.character(FIPS)) < 5, glue("0{FIPS}"), as.character(FIPS)))
数据处理上因为不想要除*党和共和党以外政党的选票(对结果无意义),所以先把他们的选票移除出了totalvotes
选取*党为基数,删除共和党数据,因为*党的选票为x%时,共和党则为(1-x)%
选用FIPS为与地图的对应码,注意原data里面FIPS有的仅为4位数,地图数据里为5位数,首位要加0
# Mapping things
us <- counties_composite()
us_map <- fortify(us, region="fips")
us_map_2016 <- data_2016 %>% right_join(us_map)
map <- ggplot() +
geom_map(data = us_map, map = us_map,
aes(x = long, y = lat, map_id = id),size = 0.05, fill = NA) +
geom_map(data = us_map_2016, map = us_map,
aes(x = long, y = lat, map_id = id, fill = percen), size = 0.05) +
scale_fill_gradient2(low = 'red', mid = 'white', high = 'blue', midpoint = 0.5) +
labs(title = "How people vote in 2016 president election",
subtitle = ("Red for Republican,Trump.\nBlue for Democrat, Hillary."),
caption = "Data: https://electionlab.mit.edu/ | Graphic: Norman WU") +
coord_map() +
theme_map(base_size = 16) +
theme(legend.title = element_text(size = 10),
plot.title = element_text(),
legend.background = element_rect(fill = NA),
plot.caption = element_text(size = 10),
legend.position = c(0.9,0.1))
ggsave(here("final.png"), width = 10, height = 6)
最后储存图片
红色代表共和党蓝色代表*党,白色基本是投票50-50
可以看出面积来看川普是大胜的,但是人口多的城市基本又全输了,这张图配合人口密度,gdp,教育情况这些图来一起看应该会很有意思,但是那么细的数据不好搞,阿拉斯加那边不知道是怎么个情况数据出不来,但没所谓了