Skip to content

Instantly share code, notes, and snippets.

@friendly
Created November 1, 2019 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friendly/f6b991fee7d628a43174d6edec328e54 to your computer and use it in GitHub Desktop.
Save friendly/f6b991fee7d628a43174d6edec328e54 to your computer and use it in GitHub Desktop.
Plots of Canadian 2019 election results
# Stacked Barplot of Support for Political Parites by Province in 2019 Canadian Federal Election
# written by Christopher D. Green
# 22 October 2019
# additions by MF 31 Oct 2019
provs<-c("BC","AB","SK","MB","ON","QC","NB","NS","PE","NF") # names of the provinces
pops<-c(5.0,4.3,1.2,1.4,14.4,8.4,0.8,1.0,0.2,0.5) # populations in millions
con<-c(34.1,69.2,64.3,45.4,33.2,16.0,32.8,25.7,27.4,28.0) # % support for Conservatives
lib<-c(26.1,13.7,11.6,26.3,41.5,34.2,37.6,41.3,43.6,44.7) # % support for Liberals
ndp<-c(24.4,11.5,19.5,20.7,16.8,10.7,9.4,18.9,7.6,23.9) # % support for NDP
grn<-c(12.4,2.8,2.5,5.1,6.2,4.4,17.4,11.0,20.8,3.1) # % support for Greens
bloc<-c(0,0,0,0,0,32.5,0,0,0,0) # % support for Bloc Quebecois
dat<-rbind(con,lib,ndp,grn,bloc)
colnames(dat) <- provs
rownames(dat) <- c("PC", "Lib", "NDP", "Grn", "Bloc")
#' ## Barplot of votes
barplot(dat,
main="% Support for Each Party, by Province in 2019 Canadian Election",
col=c("blue","red","orange","green","skyblue"),
width=pops,
names.arg=provs,
ylim=c(0,100))
# label the parties in first column of the barplot
locs <- rbind(
cumsum(dat[,1])[1:4],
c(0, cumsum(dat[,1]))[1:4]
)
locs <- colMeans(locs)
# add text labels; barplot is ugly for this.
text(3, locs, names(locs), col="white", cex=2)
#bqloc <- locator(1)
bqloc <- list(x=34.8, y=79.5)
text(bqloc$x, bqloc$y, "Bloc", col="white", cex=2)
#' ## correspondence analysis of votes
votes <- dat %*% diag(pops)
colnames(votes) <- provs
rownames(votes) <- c("PC", "Lib", "NDP", "Grn", "Bloc")
library(ca)
votes.ca <- ca(votes)
votes.ca
op <- par(cex=1.4, mar=c(5,4,2,1)+.1)
plot(votes.ca, lines=TRUE)
title(main="Support for Political Parties by Province")
par(op)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment