Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# QSS 30--Final Project
# Race Chart
## Clear file and load necessary packages
rm(list = ls())
library(dplyr)
library(tidyr)
library(ggplot2)
# setting working directory
setwd("~/Dropbox/DartmouthRace_Data")
# Three different column name vectors
col <- c("INSTNM", "PCTPELL")
temp <- read.csv("Merged2010_PP.csv")
temp2 <- temp[,col]
colnames(temp)
# Cleandata is function that reads in the different csvs one by one and makes the names of the columns from each year uniform
cleandata <- function(arg1) {
filename <- paste("Merged", arg1, "_PP.csv", sep="")
temp <- read.csv(filename,stringsAsFactors = FALSE)
temp <- temp[,col]
#go through all of the columns and make them numberic
for (i in 2:ncol(temp)){
temp[,i] <- as.numeric(temp[,i])
}
# return the new data frame
temp$year <- arg1
return(temp)
}
# Test the function
test1 <- cleandata(2008)
head(test1)
# Create empty df to add the rest to
alldata <- test1[FALSE,]
##bind each years data frame into one data framse
for (i in 2008:2014) {
temp <- cleandata(i)
alldata <- rbind(alldata,temp)
}
# I don't know what's going on with 2009, but it seems to be disorganized. I will look into this later
# alldata <- alldata[alldata$year != 2009,]
national <- alldata
head(alldata)
## Create three new data frames that contain the demographic averages for Ivy League, Dartmouth College, and National Data
ivy <- c("BROWN UNIVERSITY", "COLUMBIA UNIVERSITY IN THE CITY OF NEW YORK", "CORNELL UNIVERSITY-ENDOWED COLLEGES", "HARVARD UNIVERSITY",
"PRINCETON UNIVERSITY", "YALE UNIVERSITY","Brown University", "Columbia University",
"Cornell University", "Harvard University", "Princeton University", "Yale University")
ivyLeague <- filter(alldata, INSTNM %in% ivy)
ivyLeague <- na.omit(ivyLeague)
ivyLeague <- ivyLeague[,-1]
ivyLeague <- ivyLeague %>% group_by(year) %>% summarise_each(funs(mean))
ivyLeague$Type <- "Ivy League"
dart <- c("DARTMOUTH COLLEGE", "Dartmouth College")
dartmouth <- filter(alldata, INSTNM %in% dart)
dartmouth <- na.omit(dartmouth)
dartmouth <- dartmouth[,-1]
dartmouth <- dartmouth %>% group_by(year) %>% summarise_each(funs(mean))
dartmouth$Type <- "Dartmouth"
head(national)
national <- na.omit(national)
#national <- national[national$year != 2008,-1] # There is something up with the national data from 08. I'll check this later too
national <- national %>% group_by(year) %>% summarise_each(funs(mean))
national$Type <- "National"
national <- national[,-2]
## Bind the three data frames
df <- rbind(national, dartmouth, ivyLeague)
#df <- df[,-3]
#df <- rename(df, Male = UGDS_MEN, Female=UGDS_WOMEN)
#df1 <- gather(df, "Sex", "Percentage", 2:3, convert = TRUE)
#df$Race <- factor(df$Race, levels=c("White", "Unknown", "Black", "Asian", "Hispanic", "International", "American Indian or Alaskan Native"))
# Export for infogram
write.csv(df, "PellPctHigherEd.csv")
## Make line chart and save
plot <- ggplot(data=df, aes(x=year, y=PCTPELL, color = Type)) +
geom_line(size=2) +
geom_point(size=4) +
theme(plot.background = element_rect(fill = 'midnightblue'),
panel.grid.major = element_line(colour = "black", size=.3),
panel.grid.minor = element_line(colour = "black", size=.3),
panel.border = element_rect(fill = NA, colour = "black", size = 4),
axis.title.x = element_text(face = "bold", color = "gray87", size = 18),
axis.title.y = element_text(face = "bold", color = "gray87", size = 18),
plot.title = element_text(face = "bold", color = "gray90", size = 21),
axis.text = element_text(colour = "gray87",size=14),
strip.text = element_text(size=20),
legend.title=element_blank(),
aspect.ratio = 1) +
scale_color_manual(values=c("chartreuse4", "coral3", "cornflowerblue")) +
xlab("Years") +
ylab("Percentage of Student Population on Pell Grants") +
scale_x_continuous(breaks = c(2008, 2009, 2010 ,2011, 2012, 2013, 2014)) +
ggtitle("Pell Grants in Higher Education: Contextualizing Dartmouth's Demographics (2008-2014)")
ggsave("HigherEdSex.png", plot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.