library(tidyverse)
library(knitr)
library(lubridate)
## Get and read cabinet and party data from ParlGov database
db_file <- "parlgov-development.db"
url <- "http://www.parlgov.org/static/data/"
# download ParlGov database if not in local folder
if( ! db_file %in% list.files()) {
download.file(paste0(url, db_file), db_file, mode = "wb")
}
# retrieve data from ParlGov database tables
parlgov_db <- DBI::dbConnect(RSQLite::SQLite(), db_file)
get_parlgov_table <- function(table_name) tbl(parlgov_db, table_name) %>% collect()
party_raw <- get_parlgov_table("view_party")
elec_raw <- get_parlgov_table("view_election")
get_decade <- function(election_date) {
year(election_date) %/% 10 * 10
}
Only countries that where democratic in 1980 included. — Source: ParlGov
fam_level <- c("com", "eco", "soc", "agr", "lib", "chr", "con", "right", "other")
fam_color <- c("#FB9A99", "#33A02C", "#E31A1C", "#B2DF8A","#FDBF6F",
"#FF7F00", "#1F78B4", "#A6CEE3", "grey60")
party <- party_raw %>% mutate(family = factor(family_name_short, fam_level))
elec <-
elec_raw %>%
mutate(country = country_name,
decade = get_decade(election_date)) %>%
left_join(party %>% select(party_id, family)) %>%
filter(election_type == "parliament", ! is.na(family)) %>%
group_by(country) %>%
filter(min(election_date) <= "1980-01-01") %>%
ungroup()
turnout <-
get_parlgov_table("election") %>%
filter(id %in% elec$election_id) %>%
mutate(decade = get_decade(date)) %>%
group_by(decade) %>%
summarize(turnout = mean(votes_valid / electorate, na.rm = TRUE))
pa_fam <-
elec %>%
group_by(decade, election_id, family) %>%
summarise(vote_share = sum(vote_share, na.rm = TRUE))
pa_fam_other <-
pa_fam %>%
group_by(decade, election_id) %>%
summarise(vote_share = 100 - sum(vote_share)) %>%
mutate(family = "other")
pa_fam <- pa_fam %>% bind_rows(pa_fam_other)
# use reshape to add 0.0% for complete family data
pa_fam_dec <-
pa_fam %>%
group_by(decade, family) %>%
spread(family, vote_share, fill = 0) %>%
gather(family, vote_share, -election_id, -decade) %>%
group_by(decade, family) %>%
summarise(vote_share = mean(vote_share, na.rm = TRUE))
pa_fam_pl <-
pa_fam_dec %>%
left_join(turnout) %>%
mutate(vote_share_with_turnout = vote_share * turnout,
family = factor(family, levels = fam_level)) %>%
select(-turnout) %>%
gather(turnout, vote_share, -decade, -family)
ggplot(pa_fam_pl, aes(decade, vote_share, fill = family)) +
geom_area(position = "stack") +
scale_fill_manual(values = fam_color) +
facet_grid(. ~ turnout)
pa_fam_wide <-
pa_fam_dec %>%
mutate(vote_share = round(vote_share, 0)) %>%
spread(decade, vote_share, fill = 0)
tibble(family = fam_level) %>% left_join(pa_fam_wide)
# use long/wide transformation to add 0.0% for complete family data
pa_fam_elec <-
pa_fam %>%
group_by(election_id, family) %>%
spread(family, vote_share, fill = 0) %>%
gather(family, vote_share, -election_id, -decade) %>%
left_join(elec %>% distinct(country, election_id))
pa_fam_pl <-
pa_fam_elec %>%
inner_join(elec %>% distinct(country, election_id, election_date)) %>%
mutate(year = as.Date(election_date),
family = factor(family, levels = fam_level)) %>%
filter(election_id != 999) # Denmark 1915 (no vote share)
ggplot(pa_fam_pl, aes(year, vote_share, fill = family)) +
geom_area(position = "stack") +
scale_fill_manual(values = fam_color) +
facet_wrap(~ country)
Mean party family share by country since first election
pa_fam_ctry <-
pa_fam_elec %>%
group_by(country, family) %>%
summarise(vote_share = mean(vote_share, na.rm = TRUE) %>% round(0)) %>%
ungroup()
year_first <-
elec %>%
group_by(country) %>%
summarise(year_first = min(year(election_date))) %>%
ungroup()
pa_fam_ctry %>%
spread(family, vote_share) %>%
left_join(year_first) %>%
select(one_of(c('country', 'year_first', fam_level))) %>%
DT::datatable()
set.seed(12345)
countries <- c("Denmark", "Germany", "Italy", "Netherlands", "Norway", "United Kingdom")
pl_dt <-
pa_fam_pl %>%
ungroup() %>%
filter(country %in% countries, decade >= 1950) %>%
mutate(country = fct_shuffle(country))
pl <-
ggplot(pl_dt, aes(year, vote_share, fill = family)) +
geom_area(position = "stack") +
scale_fill_manual(values = fam_color) +
facet_wrap(~ country) +
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
) +
labs(caption = paste("Countries (randomized):", paste(countries, collapse = ", "))
)
print(pl)
ggsave("party-family-quiz.pdf", pl, width = 297, height = 210, units = "mm")