Advertisement
karstenw

Plot Age Of U.S. President Candidates 2016

Jan 8th, 2016
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.54 KB | None | 0 0
  1. pres_age <- function() {
  2.     #data
  3.     all_pres <- read.csv2("http://pastebin.com/raw/B0p5fts3", stringsAsFactors=FALSE)
  4.     all_pres[, "birthdate"] <- as.Date(strptime(all_pres[,"birthdate"], "%d.%m.%Y"))
  5.     all_pres[, "inauguration.date"] <- as.Date(strptime(all_pres[,"inauguration.date"], "%d.%m.%Y"))
  6.     all_pres[, "age_at_inauguration"] <- age_at(all_pres[, "birthdate"], all_pres[, "inauguration.date"])
  7.    
  8.     cand <- read.csv("http://pastebin.com/raw/q8vaN7ix", stringsAsFactors=FALSE, colClasses="character")
  9.     cand[, "birthday"] <- as.Date(strptime(cand[, "birthday"], "%m/%d/%Y"))
  10.     cand[, "inauguration.date"] <- as.Date("2017-01-20", "%Y-%m-%d")
  11.     cand[, "age_at_inauguration"] <- age_at(cand[, "birthday"], cand[, "inauguration.date"])
  12.    
  13.     #base plot
  14.     plot(x=1, col="white", xlim=c(0.2, 2.8), ylim=c(35,80), ylab="Age at inauguration", xlab="", axes=FALSE)
  15.     axis(2, at=seq(40,70,by=10))
  16.     axis(1, at=c(1,2), labels=c("Democrats", "Republicans"), tick=FALSE)
  17.    
  18.     #historic
  19.     max_age <- max(all_pres[, "age_at_inauguration"])
  20.     max_idx <- which(all_pres[, "age_at_inauguration"]==max_age)
  21.     abline(h=max_age, col="darkgrey", lwd=1.5)
  22.     text(x=2.8, y=max_age+1, labels=paste(all_pres[max_idx, "name"], collapse="\n"), pos=2, cex=0.6, offset=-1)
  23.    
  24.     min_age <- min(all_pres[, "age_at_inauguration"])
  25.     min_idx <- which(all_pres[, "age_at_inauguration"]==min_age)
  26.     abline(h=min_age, col="darkgrey", lwd=1.5)
  27.     text(x=2.8, y=min_age+1, labels=paste(all_pres[min_idx, "name"], collapse="\n"), pos=2, cex=0.6, offset=-1)
  28.    
  29.     mean_age <- mean(all_pres[, "age_at_inauguration"])
  30.     abline(h=mean_age, col="darkgrey", lty="dotted", lwd=1.5)
  31.     text(x=2.8, y=mean_age+1, labels="average", pos=2, cex=0.6, offset=-1)
  32.    
  33.     # candidates
  34.     set.seed(13)
  35.     col_map <- c(Democrat="blue", Republican="red")
  36.     xpos_map <- c(Democrat=1, Republican=2)
  37.     xpos <- jitter(xpos_map[cand[, "party"]], factor=2)
  38.     points(x=xpos, y=cand[, "age_at_inauguration"], col=col_map[cand[,"party"]], pch=19)
  39.     text(x=xpos, y=cand[, "age_at_inauguration"], col=col_map[cand[,"party"]], labels=cand[,"candidate"], pos=1, cex=0.6)
  40.    
  41.    
  42. }
  43.  
  44. age_at <- function(birth, at) {
  45.   if(!length(birth)==length(at)) stop("birth and at need same length")
  46.   if(!inherits(birth, "Date")) stop("birth needs to be a Date")
  47.   if(!inherits(at, "Date")) stop("at needs to be a Date")
  48.  
  49.   res <- rep(NA, length(birth))
  50.   for (i in seq_along(birth)) res[i] <- length(seq(birth[i], at[i], "years"))-1
  51.   return(res)
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement