2 min read

Votes and seats code

This code builds examples of the graphs in this post.

rm(list = ls())

library(tidyverse)
library(readxl)
library(ggthemes)

# Download the Comparative Parties dataset from this link:
# http://www.marquette.edu/polisci/documents/PARTY19502011.xls

# Read the file, change the file path accordingly
d <- read_excel("/YOUR/PATH/TO/PARTY19502011.xls")

elect <- d[d$elect1==1,] #  subset years when an election took place
elect$date <- as.Date(paste(elect$year, elect$elmon, elect$elday, sep = "-")) # make date variable

# Subset just the vote share variables
votes <- elect[,c("coid","date","leftv","rightv","mcdemv","centv","rwpvote","llvote")]
votes$leftv <- votes$leftv - votes$llvote #   Recode "left votes" to exclude left-libertarian
votes$rightv <- votes$rightv - votes$rwpvote #  Recode "right votes" to exclude right-wing populist

# Collapse votes data.frame into key-value pairs
votes <- gather(votes, key = "PartyCode", value = "votes", -coid, -date)
# Now party type is a variable (named PartyCode) and all vote shares are in one column

# Make "PartyType" variable. It's the same as "PartyCode" but with nice names
votes$PartyType <- ""
votes$PartyType[votes$PartyCode=="leftv"] <- "Left"
votes$PartyType[votes$PartyCode=="rightv"] <- "Right"
votes$PartyType[votes$PartyCode=="mcdemv"] <- "Centrist Christian Dems"
votes$PartyType[votes$PartyCode=="centv"] <- "Secular centrists"
votes$PartyType[votes$PartyCode=="rwpvote"] <- "Right-wing Populist"
votes$PartyType[votes$PartyCode=="llvote"] <- "Left Libertarian"

votes$votes <- votes$votes/100 #  convert votes % to proportion

# Make a plot for Australia
australia.votes <- ggplot(votes[votes$coid==1,], 
                          aes(date, votes, col=PartyType)) +
  geom_point() +
  geom_line(size = 1.5) +
  scale_y_continuous(limits = c(0,1), labels = scales::percent, name = NULL) +
  scale_x_date(name = NULL) +
  labs(title = "Australia",
       subtitle = "Vote share by party ideology") +
  theme_tufte() +
  theme(legend.position = "bottom",
        legend.title = element_blank())
print(australia.votes)

################################################################################################
# Repeat the above process for seats instead of votes

seats <- elect[,c("coid","date","lefts","rights","mcdems","cents","rwpseat","llseat")]
seats$lefts <- seats$lefts - seats$llseat
seats$rights <- seats$rights - seats$rwpseat
seats <- gather(seats, key = "PartyCode", value = "seats", -coid, -date)
seats$PartyType <- ""
seats$PartyType[seats$PartyCode=="lefts"] <- "Left"
seats$PartyType[seats$PartyCode=="rights"] <- "Right"
seats$PartyType[seats$PartyCode=="mcdems"] <- "Centrist Christian Dems"
seats$PartyType[seats$PartyCode=="cents"] <- "Secular centrists"
seats$PartyType[seats$PartyCode=="rwpseat"] <- "Right-wing Populist"
seats$PartyType[seats$PartyCode=="llseat"] <- "Left Libertarian"
seats$seats <- seats$seats/100

australia.seats <- ggplot(seats[seats$coid==1,], aes(date, seats, col=PartyType)) +
  geom_point() +
  geom_line(size = 1.5) +
  scale_y_continuous(limits = c(0,1), labels = scales::percent, name = NULL) +
  scale_x_date(name = NULL) +
  labs(title = "Australia",
       subtitle = "Seat share by party ideology") +
  theme_tufte() +
  theme(legend.position = "bottom",
        legend.title = element_blank())
print(australia.seats)