自1798年以来美国总统选举结果的可视化

自1798年以来美国总统选举结果的可视化

这是学习zonination/election-history的笔记,不过不能完全理解美国的总统选举制度变迁,所以有些代码的背后含义无法理解。

首先是本次绘图需要的数据集:
elec.csv

这个数据包含了每届选举中各州的选举结果。

首先进行一些整理,总的来说,我们需要四个变量,第一个变量是选举年份,作为x轴,第二个变量是各州结果,作为y轴,第三个是选举结果,作为填充颜色,第四个是Margin,是表示执政党获选的优势度(百分比票数差),作为填充透明度。最后还需要各个州位于的区域分类。用于分面。

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
elec <- read.csv("elec.csv", stringsAsFactors = F)
library(ggplot2)
library(gridExtra)

elec$Margin <- NA
elec$Region <- NA
elec$BEA <- NA
elec$Admission <- NA
elec$State.yr <- NA

for(n in 1:nrow(elec)){
col <- sort(c(
as.numeric(as.character(elec[n, c(4, 7, 10, 13)][1, 1])),
as.numeric(as.character(elec[n, c(4, 7, 10, 13)][1, 2])),
as.numeric(as.character(elec[n, c(4, 7, 10, 13)][1, 3])),
as.numeric(as.character(elec[n, c(4, 7, 10, 13)][1, 4]))
), decreasing = T)
elec$Margin[n] <- (col[1] - col[2])/sum(col)
# 对于仅由州议会和选举人团决定的选举和州议会决定的选举把margin减小50%:
if(elec$Notes[n] != ""){
elec$Margin <- elec$Margin * 0.5
}
# 下面是对州的分类:北部、中部、南部、西部
if(elec$State[n]=="Connecticut"){elec$Region[n]<-"Northeast";elec$BEA[n]<-"New England";elec$Admission[n]=1788.2}
if(elec$State[n]=="Maine"){elec$Region[n]<-"Northeast";elec$BEA[n]<-"New England";elec$Admission[n]=1820}
if(elec$State[n]=="Massachusetts"){elec$Region[n]<-"Northeast";elec$BEA[n]<-"New England";elec$Admission[n]=1788.3}
if(elec$State[n]=="New Hampshire"){elec$Region[n]<-"Northeast";elec$BEA[n]<-"New England";elec$Admission[n]=1788.6}
if(elec$State[n]=="Rhode Island"){elec$Region[n]<-"Northeast";elec$BEA[n]<-"New England";elec$Admission[n]=1790}
if(elec$State[n]=="Vermont"){elec$Region[n]<-"Northeast";elec$BEA[n]<-"New England";elec$Admission[n]=1791}
if(elec$State[n]=="New Jersey"){elec$Region[n]<-"Northeast";elec$BEA[n]<-"Mideast";elec$Admission[n]=1787.3}
if(elec$State[n]=="New York"){elec$Region[n]<-"Northeast";elec$BEA[n]<-"Mideast";elec$Admission[n]=1788.8}
if(elec$State[n]=="Pennsylvania"){elec$Region[n]<-"Northeast";elec$BEA[n]<-"Mideast";elec$Admission[n]=1787.2}
# Midwest
if(elec$State[n]=="Illinois"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Great Lakes";elec$Admission[n]=1818}
if(elec$State[n]=="Indiana"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Great Lakes";elec$Admission[n]=1816}
if(elec$State[n]=="Michigan"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Great Lakes";elec$Admission[n]=1837}
if(elec$State[n]=="Ohio"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Great Lakes";elec$Admission[n]=1803}
if(elec$State[n]=="Wisconsin"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Great Lakes";elec$Admission[n]=1848}
if(elec$State[n]=="Iowa"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Plains";elec$Admission[n]=1846}
if(elec$State[n]=="Kansas"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Plains";elec$Admission[n]=1861}
if(elec$State[n]=="Minnesota"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Plains";elec$Admission[n]=1858}
if(elec$State[n]=="Missouri"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Plains";elec$Admission[n]=1821}
if(elec$State[n]=="Nebraska"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Plains";elec$Admission[n]=1867}
if(elec$State[n]=="North Dakota"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Plains";elec$Admission[n]=1889.1}
if(elec$State[n]=="South Dakota"){elec$Region[n]<-"Midwest";elec$BEA[n]<-"Plains";elec$Admission[n]=1889.2}
# South
if(elec$State[n]=="Delaware"){elec$Region[n]<-"South";elec$BEA[n]<-"Mideast";elec$Admission[n]=1787.1}
if(elec$State[n]=="Dist. of Col."){elec$Region[n]<-"South";elec$BEA[n]<-"Mideast";elec$Admission[n]=1961} # Admission based on 23rd Amendment
if(elec$State[n]=="Florida"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1845.1}
if(elec$State[n]=="Georgia"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1788.1}
if(elec$State[n]=="Maryland"){elec$Region[n]<-"South";elec$BEA[n]<-"Mideast";elec$Admission[n]=1788.4}
if(elec$State[n]=="North Carolina"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1789}
if(elec$State[n]=="South Carolina"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1788.5}
if(elec$State[n]=="Virginia"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1788.7}
if(elec$State[n]=="West Virginia"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1863}
if(elec$State[n]=="Alabama"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1819}
if(elec$State[n]=="Kentucky"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1792}
if(elec$State[n]=="Mississippi"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1817}
if(elec$State[n]=="Tennessee"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1796}
if(elec$State[n]=="Arkansas"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1836}
if(elec$State[n]=="Louisiana"){elec$Region[n]<-"South";elec$BEA[n]<-"Southeast";elec$Admission[n]=1812}
if(elec$State[n]=="Oklahoma"){elec$Region[n]<-"South";elec$BEA[n]<-"Southwest";elec$Admission[n]=1907}
if(elec$State[n]=="Texas"){elec$Region[n]<-"South";elec$BEA[n]<-"Southwest";elec$Admission[n]=1845.2}
# West
if(elec$State[n]=="Arizona"){elec$Region[n]<-"West";elec$BEA[n]<-"Southwest";elec$Admission[n]=1912.2}
if(elec$State[n]=="Colorado"){elec$Region[n]<-"West";elec$BEA[n]<-"Mountain";elec$Admission[n]=1876}
if(elec$State[n]=="Idaho"){elec$Region[n]<-"West";elec$BEA[n]<-"Mountain";elec$Admission[n]=1890.1}
if(elec$State[n]=="Montana"){elec$Region[n]<-"West";elec$BEA[n]<-"Mountain";elec$Admission[n]=1889.3}
if(elec$State[n]=="Nevada"){elec$Region[n]<-"West";elec$BEA[n]<-"Far West";elec$Admission[n]=1864}
if(elec$State[n]=="New Mexico"){elec$Region[n]<-"West";elec$BEA[n]<-"Southwest";elec$Admission[n]=1912.1}
if(elec$State[n]=="Utah"){elec$Region[n]<-"West";elec$BEA[n]<-"Mountain";elec$Admission[n]=1896}
if(elec$State[n]=="Wyoming"){elec$Region[n]<-"West";elec$BEA[n]<-"Mountain";elec$Admission[n]=1890.2}
if(elec$State[n]=="Alaska"){elec$Region[n]<-"West";elec$BEA[n]<-"Far West";elec$Admission[n]=1959.1}
if(elec$State[n]=="California"){elec$Region[n]<-"West";elec$BEA[n]<-"Far West";elec$Admission[n]=1850}
if(elec$State[n]=="Hawaii"){elec$Region[n]<-"West";elec$BEA[n]<-"Far West";elec$Admission[n]=1959.2}
if(elec$State[n]=="Oregon"){elec$Region[n]<-"West";elec$BEA[n]<-"Far West";elec$Admission[n]=1859}
if(elec$State[n]=="Washington"){elec$Region[n]<-"West";elec$BEA[n]<-"Far West";elec$Admission[n]=1889.4}

elec$State.yr[n] <- paste("(", elec$Admission, ")", elec$State[n], sep = "")
}; rm(col); rm(n)

然后绘制第一幅图,州按照字母表顺序排列:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
elec$State <- factor(elec$State)
theme_set(theme_bw(base_family = "STSong") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5)))
ggplot(elec, aes(x = Year, y = State)) +
geom_tile(aes(fill = Party, color = Party, alpha = Margin), na.rm = T) +
# geom_point(aes(color = Party), size = 1) +
scale_fill_manual(values = c("#377eb8","#984ea3","#ff7f00","#4daf4a","#e41a1c","#e6ab02")) +
scale_color_manual(values = c("#377eb8","#984ea3","#ff7f00","#4daf4a","#e41a1c","#e6ab02")) +
geom_text(aes(label = paste(substring(Party, 1, 1), substring(Party, 12, 12), sep = ""), color = Party), fontface = "bold", size = 2) +
guides(alpha = "none") +
labs(title = "图:按州选举结果",
subtitle = "州按照字母表顺序排列",
x = "选举年份", y = "") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
theme(strip.text.y = element_text(size = 8)) +
theme(legend.position = "bottom") +
geom_vline(xintercept = 9.5, linetype = 4) +
annotate("text", x = 9.5, y = 1, label = "(Margin by Electoral College)", angle = 90, hjust = 0, vjust = -0.5, size = 3) +
annotate("text", x = 9.5, y = 1, label = "(Margin by Popular Vote)", angle = 90, hjust = 0, vjust = 1.5, size = 3)

第二幅图,按照Admission(不理解是什么)进行排序:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
elec$State <- reorder(elec$State, -elec$Admission)
ggplot(elec, aes(x = Year, y = State)) +
geom_tile(aes(fill = Party, color = Party, alpha = Margin), na.rm = T) +
scale_fill_manual(values = c("#377eb8","#984ea3","#ff7f00","#4daf4a","#e41a1c","#e6ab02")) +
scale_color_manual(values = c("#377eb8","#984ea3","#ff7f00","#4daf4a","#e41a1c","#e6ab02")) +
geom_text(aes(label = paste(substring(Party, 1, 1), substring(Party, 12, 12), sep = ""), color = Party), fontface = "bold", size = 2) +
guides(alpha = "none") +
guides(alpha = "none") +
labs(title = "图:按州选举结果",
subtitle = "州按照Admission排序",
x = "选举年份", y = "") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
theme(strip.text.y = element_text(size = 8)) +
theme(legend.position = "bottom") +
geom_vline(xintercept = 9.5, linetype = 4) +
annotate("text", x = 9.5, y = 1, label = "(Margin by Electoral College)", angle = 90, hjust = 0, vjust = -0.5, size = 3) +
annotate("text", x = 9.5, y = 1, label = "(Margin by Popular Vote)", angle = 90, hjust = 0, vjust = 1.5, size = 3)

最后一幅图是按照州位于的地理区域进行分面:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
ggplot(elec, aes(x = Year, y = State)) +
geom_tile(aes(fill = Party, color = Party, alpha = Margin), na.rm = T) +
scale_fill_manual(values = c("#377eb8","#984ea3","#ff7f00","#4daf4a","#e41a1c","#e6ab02")) +
scale_color_manual(values = c("#377eb8","#984ea3","#ff7f00","#4daf4a","#e41a1c","#e6ab02")) +
geom_text(aes(label = paste(substring(Party, 1, 1), substring(Party, 12, 12), sep = ""), color = Party), fontface = "bold", size = 2) +
guides(alpha = "none") +
labs(title = "图:按州选举结果",
subtitle = "州按地区分面",
x = "选举年份", y = "") +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
theme(strip.text.y = element_text(size = 8)) +
facet_grid(BEA ~ ., scales = "free_y", space = "free_y") +
geom_vline(xintercept = 9.5, linetype = 4)

# R

评论

Your browser is out-of-date!

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

×