Advertisement
Guest User

Untitled

a guest
Jan 10th, 2020
4,725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.98 KB | None | 0 0
  1. setwd("~/Documents/USNews")
  2.  
  3. #read in data + key for different naming conventions betw. d1 vs. (d2, d3)
  4. d1 <- read.csv("usnews_83-07.csv", check.names=FALSE); rownames(d1) <- d1[,1]; d1 <- d1[-58,-1]; colnames(d1)
  5. d2 <- read.csv("usnews_08-15.csv", check.names=FALSE); rownames(d2) <- d2[,1]; d2 <- d2[,-1]; d2 <- d2[,1:5]; colnames(d2)
  6. d3 <- read.csv("usnews_13-20.csv", check.names=FALSE); rownames(d3) <- d3[,1]; d3 <- d3[,-1]; colnames(d3)
  7. matcher <- read.csv("usnews_namematch.csv", header = F)
  8.  
  9. #add in schools not in key / d1
  10. addInNewSchools <- T
  11. if(addInNewSchools){
  12.   notInD1 <- setdiff(unique(rownames(d2), rownames(d3)), matcher[,2])
  13.   matcher <- as.data.frame(cbind(c(as.character(matcher$V1), notInD1), c(as.character(matcher$V2), notInD1)))
  14. }
  15.  
  16. #make some names nicer
  17. matcher <- as.matrix(matcher)
  18. matcher[matcher[,1] == "Northeastern",1] <- "Northeastern University"
  19. matcher[matcher[,1] == "Boston Univ",1] <- "Boston University"
  20. matcher[matcher[,1] == "Georgia",1] <- "University of Georgia"
  21. matcher <- as.data.frame(matcher)
  22.  
  23. #compile main data frame
  24. d <- t(sapply(1:length(matcher[,1]), function(x) c(d1[rownames(d1) == matcher[x,1],], d2[rownames(d2) == matcher[x,2],], d3[rownames(d3) == matcher[x,2],])))
  25. rownames(d) <- matcher[,1]
  26. d <- as.data.frame(d)
  27.  
  28. #get positions for names in right margin (since US News allows ties)
  29. name_positions <- orig_name_positions <- sort(unlist(d[,"2020"]))
  30. for(i in 2:length(name_positions)){if(name_positions[i] <= name_positions[i-1]){name_positions[i] <- name_positions[i-1] + 1}}
  31. name_positions[-(1:15)] <- name_positions[-(1:15)] - 1
  32.  
  33. #set plotting window properties
  34. par(mar = c(6,6,0,15))
  35. par(xpd=TRUE)
  36.  
  37. #specify colors for rainbow effect or not
  38. cols <- rep(1, length(d[,1]))
  39.  
  40. #subset schools for easier viewing
  41. subsetD <- T
  42. maxRank <- 50
  43. schoolSize <- 1.2
  44. if(subsetD){
  45.   d <- d[as.numeric(which(d$`2020` < (maxRank + 1))),]
  46. }
  47.  
  48. #throw away data for small handful of low-ranked schools
  49. d <- d[sapply(1:length(d[,1]), function(x) which(rownames(d) == labels(name_positions)[x])),]
  50. d[d > 65] <- NA
  51.  
  52. #draw plots
  53. dir.create("plots")
  54. reverseVertAxis <- T
  55. for(j in nrow(d):1){
  56.   print(paste0(j, ": ", rownames(d)[j]))
  57.   png(filename = paste0("plots/", j, "_", rownames(d)[j], ".png"), width = 1200, height = 1200)
  58.   par(xpd=TRUE)
  59.   if(reverseVertAxis){
  60.     ylims <- c(65,1)
  61.     par(mar = c(0,5,5,20.5))
  62.   } else {
  63.     ylims <- c(1,65)
  64.     par(mar = c(3,5,0,20.5))
  65.   }
  66.   plot(1,1, bty="n", xlim = c(1984,2020), ylim = ylims, type = "n", xaxt = "n", yaxt="n", xlab = "", ylab = "")
  67.   title(ylab="Rank", cex.lab=2)
  68.   title(xlab = "Year", line=1.5, cex.lab=2)
  69.   axis(side = 2, at = c(1:13*5-4), cex.axis=1.5)
  70.   if(reverseVertAxis){
  71.     title("U.S. News National University Rankings (1983 - 2020)", line = 1, adj = 0.00, cex.main = 3)
  72.     axis(side = 3, at = 1983:2020, pos = 0, cex.axis = 1.25)
  73.   } else {
  74.     title("U.S. News National University Rankings (1983 - 2020)", line = -4, adj = 0.05, cex.main = 3)
  75.     axis(side = 1, at = 1983:2020, pos = 0, cex.axis = 1.25)  
  76.   }
  77.     for(i in (1:length(d[,1]))[-j]){
  78.       years <- as.numeric(colnames(d))
  79.       rank <- d[i,]
  80.       name <- rownames(d)[i]
  81.       color <- cols[i]
  82.       line_width <- 1
  83.       lines(years[!is.na(rank)], rank[!is.na(rank)], col = color, lwd = line_width)
  84.       textStart <- 2020
  85.       if(sum(orig_name_positions[labels(orig_name_positions) == name] == orig_name_positions) > 1){
  86.         textStart <- 2022
  87.         segments(x0 = 2020, y0 = orig_name_positions[labels(orig_name_positions) == name],
  88.                  x1 = 2022, y1 = name_positions[labels(name_positions) == name], lty = 3, lwd = 1.5)
  89.       }
  90.       text(name, x = textStart, y = name_positions[labels(name_positions) == name], pos = 4, col = color, cex = schoolSize)
  91.     }
  92.   color = 2
  93.   line_width <- 3
  94.   years <- as.numeric(colnames(d))
  95.   rank <- d[j,]
  96.   name <- rownames(d)[j]
  97.   lines(years[!is.na(rank)], rank[!is.na(rank)], col = color, lwd = line_width)
  98.   textStart <- 2020
  99.   if(sum(orig_name_positions[labels(orig_name_positions) == name] == orig_name_positions) > 1){
  100.     textStart <- 2022
  101.     segments(x0 = 2020, y0 = orig_name_positions[labels(orig_name_positions) == name], col = color,
  102.              x1 = 2022, y1 = name_positions[labels(name_positions) == name], lty = 3, lwd = 1.5)
  103.   }
  104.   text(name, x = textStart, y = name_positions[labels(name_positions) == name], pos = 4, col = color, cex = schoolSize)
  105.   dev.off()
  106. }
  107.  
  108. #put together gif
  109. library(magick)
  110. library(gtools)
  111. library(purrr)
  112.  
  113. if(reverseVertAxis){
  114.   fileNames <- mixedsort(list.files(path = "plots/", pattern = "*.png", full.names = T))
  115. } else {
  116.   fileNames <- rev(mixedsort(list.files(path = "plots/", pattern = "*.png", full.names = T)))
  117. }
  118.  
  119. for(i in 1:length(fileNames)){
  120.   image_write(image_crop(image = image_read(fileNames[i]), geometry = "1200x1150"), fileNames[i])
  121. }
  122.  
  123. fileNames %>%
  124.   map(image_read) %>%
  125.   image_join() %>%  
  126.   image_animate(fps=1) %>%
  127.   image_write(path = "schools.gif", format = "gif")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement