carjed
12/14/2016 - 4:55 PM

HarryPaletteAndThePrisonRofAzkaban.r

##############################################################################
# I would call this a novelty script if I wasn't dead serious about using
# Hogwarts house colors in scientific writing
#
# To be clear, I am 100% dead serious about using Hogwarts house colors in
# scientific writing
#
# You can load this gist directly into R with the following commands
# install.packages('devtools')
# require(devtools)
# devtools::source_gist("3696d9dba37c9276ea33aeced69e733e", filename = "HarryPaletteAndThePrisonRofAzkaban.r")
##############################################################################

require(ggplot2)
require(dplyr)

# Raw (hex) color palette vectors, based on the house crests at
# http://harrypotter.wikia.com/wiki/Hogwarts_Houses
# The first set is derived from http://www.palettefx.com/beta/
godric <- c("#aa1122", "#eecc55", "#886600", "#882211", "#330011")
helga <- c("#ffeecc", "#ffee88", "#ddcc44", "#aa9933", "#555500")
rowena <- c("#bbddee", "#88aaee", "#5566aa", "#222288", "#001122")
salazar <- c("#119977", "#665577", "#113322", "#110022", "#779988")

# A second set of palettes, selected from http://palettegenerator.com/
# (I like these better, but they are in a rather arbitrary order)
godric2 <- c("#F4D75C", "#692A0E", "#FCF4D1", "#D5AB3F", "#AD8539", "#BE3A34", "#8A5320", "#F1DDA3", "#C8AF79")
helga2 <- c("#141111", "#32302F", "#F0E680", "#F7F1AA", "#4E4E58", "#7A7B89", "#BDB272", "#7D723F", "#B5B8C1")
rowena2 <- c("#2B292F", "#0D0D18", "#4F494A", "#C8DBEE", "#242F6E", "#726A6A", "#A4B5D0", "#A19896", "#6879A3")
salazar2 <- c("#0D5E41", "#4D5B5A", "#111716", "#343439", "#6D757A", "#879995", "#2D9C75", "#E5EAD7", "#B0BBBA")

# Plot palette colors with function accioPalette("palette", c("house1", "house2", ...))
# Default is the 9-color hogwarts2 palettes for all 4 houses
# Case-insensitive abbreviations for house names are accepted 
#     (e.g., c("g", "r") is equivalent to c("Gryffindor", "Ravenclaw"))
accioPalette<-function(pal="hogwarts2", houses=c("Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin")){
  
  if(pal=="hogwarts1"){
    ind=5
    cols=c(godric, helga, rowena, salazar)
  } else if(pal=="hogwarts2"){
    ind=9
    cols=c(godric2, helga2, rowena2, salazar2)
  } else {
    stop("invalid palette name")
  } 
  
  if (length(houses)>4){
    stop("Please enter up to four Hogwarts houses, you muggle.")
  }
  
  house.list<-c("Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin")
  house.selection<-list()
  
  for(i in 1:length(houses)){
    grepstr <- paste0("^", houses[i])
    if(!any(grepl(grepstr, house.list, ignore.case=T))){
      stop("You entered an invalid house name. Houses in other schools (Beauxbatons, Castelobruxo, Durmstrang, Ilvermorny, Koldovstoretz, Mahoutokoro, and Uagadou) are not currently supported.")
    } else {
      house.selection[i] <- house.list[which(grepl(grepstr, house.list, ignore.case=T))]
    }
  }
  
  house.selection <- unique(unlist(house.selection))
  
  # Dummy data for plot
  hogwarts <- data.frame(
    x=rep(letters[1:ind], 4),
    house=rep(c("Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"), each=ind),
    cols=cols
  )
  
  plotdat <- hogwarts %>%
    dplyr::filter(house %in% house.selection)
  
  plotcols <- plotdat$cols
  
  ggplot(plotdat, aes(x=x, y=1))+
    geom_bar(stat="identity", fill=plotcols)+
    facet_wrap(~house)+
    scale_y_continuous(limits=c(0,2))+
    geom_text(aes(y=1.5, label=cols), angle=90)+
    theme_classic()+
    theme(axis.ticks=element_blank(),
          axis.text=element_blank(),
          axis.title=element_blank())
}