-
Notifications
You must be signed in to change notification settings - Fork 0
/
US_presidential_and_senate_elections_2020.R
133 lines (103 loc) · 5 KB
/
US_presidential_and_senate_elections_2020.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
## Data source: https://electionlab.mit.edu/data
# For this analysis, I have used a wide range of packages. For the graph, I've used plot_usmap which populates the 51 US states.
Main packages used.
- readxl,readr: to upload the ddata.
- lubridate: work with dates, particularly year.
- tidyverse: data manipulation and wrangling
- ggplot2, ggtext
- systemfonts, sysfonts,showtext: to upload the font
- choroplethr,choroplethrMaps,choroplethrZip,mapproj,usmap: mapping
- scales: format of variables
- hrbrthemes: graph theme
##### Font used is Lora
# Font --------------------------------------------------------------------
font_add_google("Lora")
font_labels <- "Lora"
showtext_auto()
##### Code for the first graph
# Presidential ------------------------------------------------------------
first_map<-plot_usmap("data_source", values = "party_simplified",labels = TRUE) +
scale_fill_manual(values = map_fills) +
labs(fill = "party_simplified") +
labs(x = "",y = "",
title = "US Presidential election 2020") +
theme(
plot.title = element_text(margin = margin(b = 15, t=10),
color = "#000000",face = "bold",size = 14,
hjust = 0.5,
family = font_labels),
legend.position = "none",
legend.title=element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.x=element_blank(),
panel.grid.major = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "#fbfaf6", color = NA), # color removes the border
axis.ticks = element_blank()
) + guides(fill = guide_legend(
label.position = "bottom",
family = font_labels,
color = "#808080",
keywidth = 3, keyheight = 0.5)) +
annotate("text", x = -1000, y = -2700000, hjust = 0, vjust = 1,fontface=3, size = 8, colour = "#1a6aff",
family = font_labels, label = str_wrap("Biden - 306")) +
annotate("text", x = 1240000, y = -2700000, hjust = 0, vjust = 1,fontface=3, size = 8, colour = "#ff4a43",
family = font_labels, label = str_wrap("Trump - 232"))
# Senate ------------------------------------------------------------------
second_map<-plot_usmap("data_source", values = "party_simplified",labels = TRUE) +
scale_fill_manual(values = map_fills,labels = c("Democratic", "Republican", "No election")) +
labs(fill = "party_simplified") +
labs(x = "",y = "",
title = "US Senate election 2020",
caption = "Source:https://electionlab.mit.edu/data\nVisualization: JuanmaMN (Twitter @Juanma_MN)",
x = "",
y = "") +
theme(
plot.title = element_text(margin = margin(b = 15, t=10),
color = "#000000",face = "bold",size = 14,
hjust = 0.5,
family = font_labels),
plot.caption = element_text(margin = margin(t = 20,r=20),
color = "#000000", size = 10, family = font_labels),
legend.position = "none",
legend.title=element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks.x=element_blank(),
panel.grid.major = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
legend.margin=margin(b = 2, unit='cm'),
legend.text=element_text(size=8,family = font_labels),
legend.key.size = unit(0.3, "cm"),
legend.background = element_rect(fill = "#fbfaf6", color = NA),
plot.background = element_rect(fill = "#fbfaf6", color = NA), # color removes the border
axis.ticks = element_blank()
#plot.background = element_rect(fill = "#f7f7f7")
) + guides(fill = guide_legend(
label.position = "bottom",
family = font_labels,
color = "#808080",
keywidth = 3, keyheight = 0.5)) +
annotate("text", x = -1000, y = -2700000, hjust = 0, vjust = 1,fontface=3, size = 8, colour = "#1a6aff",
family = font_labels, label = str_wrap("Biden - 50")) +
annotate("text", x = 1240000, y = -2700000, hjust = 0, vjust = 1,fontface=3, size = 8, colour = "#ff4a43",
family = font_labels, label = str_wrap("Trump - 50"))
# Arrange -----------------------------------------------------------------
ggarrange(first_map,second_map, ncol=2, nrow=1, common.legend = F, legend="none") +
theme_ipsum() +
labs(x = "",y = "",
caption = "Source: https://electionlab.mit.edu/data\nVisualization: JuanmaMN (Twitter @Juanma_MN)") +
theme(
plot.caption = element_text(margin = margin(t = 20, b = 10),
color = "#000000", size = 8, family = font_labels,
hjust = 0.95),
plot.background = element_rect(fill = "#fbfaf6", color = NA))