-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsenate_debut_example.R
50 lines (38 loc) · 1.38 KB
/
senate_debut_example.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
# If necessary:
# library(devtools)
# install_github('arvi1000/rGridMap')
library(rGridMap)
library(rvest)
library(lubridate)
library(data.table)
library(ggplot2)
## PART ONE: Organize data ----
# scrape wiki table on US senators
url <- 'https://en.wikipedia.org/wiki/List_of_current_United_States_Senators'
wiki_page <- html(url)
wiki_table <-
wiki_page %>% html_nodes('table') %>% .[[6]] %>% html_table()
# clean up names
names(wiki_table) <-
names(wiki_table) %>% gsub('\\s|/', '_', .) %>% tolower
# get two letter abbrevations from state.name
setDT(wiki_table)
wiki_table[, state.abb := state.abb[match(state, state.name)]]
# data frame of year senior senator began term, by state
plot_dat <-
wiki_table[, .(began= min(year(mdy(assumed_office)))), by=state.abb]
# make a label column
plot_dat[, label:=paste0(state.abb, '\n', began)]
plot_dat[, began_decade := cut(began,
breaks = seq(1970, 2020, 10),
labels = paste0(seq(1970, 2010, 10), 's'))]
## PART TWO: Make grid map ----
# build
senate_debut_gg <-
plotGridMap(plot_dat, fill_var = 'began_decade', label_var = 'label') +
scale_fill_brewer(palette = 4) +
labs(title ='Term Debut of Senior Senator, by State', fill = 'Decade')
# print
senate_debut_gg
# optional, save to file:
ggsave('rGridMap/example/senate_debut.png', senate_debut_gg, width = 8, height = 6)