Skip to content

Instantly share code, notes, and snippets.

@aaronferrucci
Last active January 20, 2020 06:16
Show Gist options
  • Save aaronferrucci/dae0b1b70b5cd6bf5cfaeccf0b745686 to your computer and use it in GitHub Desktop.
Save aaronferrucci/dae0b1b70b5cd6bf5cfaeccf0b745686 to your computer and use it in GitHub Desktop.
plot democratic presidential elections, election year vs. candidate age
library(ggplot2)
library(scales) # for oob
winners <- data.frame(
candidate=c("Kennedy", "Kennedy", "Carter", "Clinton", "Clinton", "Obama", "Obama"),
age=c(43, 47, 52, 46, 50, 47, 51),
year=c(1960, 1964, 1976, 1992, 1996, 2008, 2012),
result=c("win")
)
losers <- data.frame(
candidate=c("Humphrey", "McGovern", "Carter", "Mondale", "Dukakis", "Gore", "Kerry", "Clinton"),
age=c(47, 50, 56, 56, 55, 52, 61, 69),
year=c(1968, 1972, 1980, 1984, 1988, 2000, 2004, 2016),
result=c("lose")
)
data <- rbind(winners, losers)
p <- ggplot(data=data, aes(x=year, y=age, fill=result)) +
scale_x_continuous(breaks=seq(min(data$year), max(data$year), 8)) +
scale_y_continuous(limits=c(35, NA), oob=rescale_none) + # w/o oob assignment, bars don't display
geom_bar(stat="identity") +
geom_text(aes(label=candidate), angle=90, vjust=0.2, hjust=1.2, size=4) +
xlab("election year") +
ylab("candidate age")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment