Let’s learn how to access, explore and visualize US political data via publicly available resources according to (jaytimm). # Basic infomation on lawmakers You can find basic information on lawmakers via (DATA and LAB).
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3 ✓ purrr 0.3.4
## ✓ tibble 3.0.4 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
# load the data
# 全URLを書いてもよいですが、長くなるので共通部分を取り出している。
git <- 'https://raw.githubusercontent.com/CivilServiceUSA/'
senate_lawmakers <- jsonlite::fromJSON(url(paste0(git, "us-senate/master/us-senate/data/us-senate.json")))
house_lawmakers <- jsonlite::fromJSON(url(paste0(git, 'us-house/master/us-house/data/us-house.json')))
# show the five lawwmakers
# selectで列を選んで、filterで行を選ぶ。
house_lawmakers %>%
select(name, state_code, party, gender, ethnicity, twitter_handle) %>%
filter(name %in% # name列の4人の名前を含む行を取り出す。
c("Rashida Tlaib",
"Ilhan Omar",
"Ayanna Pressley",
"Alexandria Ocasio-Cortez")
)
## name state_code party gender ethnicity
## 1 Ayanna Pressley MA democrat female white-american
## 2 Rashida Tlaib MI democrat female multi-racial-american
## 3 Ilhan Omar MN democrat female middle-eastern-american
## 4 Alexandria Ocasio-Cortez NY democrat female multi-racial-american
## twitter_handle
## 1 housedemocrats
## 2 housedemocrats
## 3 housedemocrats
## 4 housedemocrats
1.House
# age of the House
house_lawmakers %>%
mutate(yearsHouse =
lubridate::year(as.Date(Sys.Date())) -
lubridate::year(as.Date(date_of_birth))) %>%
as_tibble() %>% # summarizeを使用するためにtibbleに変換する。
summarize("平均" = mean(.$yearsHouse),
"最高" = max(.$yearsHouse),
"最低" = min(.$yearsHouse)
)
## # A tibble: 1 x 3
## 平均 最高 最低
## <dbl> <dbl> <dbl>
## 1 58.7 88 26
2.Senate
# age of the Senate
senate_lawmakers %>%
mutate(yearsSenate =
lubridate::year(as.Date(Sys.Date())) -
lubridate::year(as.Date(date_of_birth))) %>%
as_tibble() %>% # summarizeを使用するためにtibbleに変換する。
summarize("平均" = mean(.$yearsSenate),
"最高" = max(.$yearsSenate),
"最低" = min(.$yearsSenate)
)
## # A tibble: 1 x 3
## 平均 最高 最低
## <dbl> <dbl> <dbl>
## 1 64.8 88 42
1.House
# histogram for the house
house_lawmakers %>%
mutate(yearsHouse =
lubridate::year(as.Date(Sys.Date())) -
lubridate::year(as.Date(date_of_birth))) %>%
ggplot (aes(yearsHouse)) +
geom_histogram(bins=20, fill = 'steelblue', alpha = .85) +
labs(title = 'The 117th House composition by age',
caption = 'Data source: CivilServiceUSA')
2.Senate
Note that the echo = FALSE
parameter was added to the code chunk to prevent printing of the R code that generated the plot.