Last active
October 16, 2016 22:13
-
-
Save morganmelon/eec6ab96f1f4a5b2a716baa82bc76ce9 to your computer and use it in GitHub Desktop.
Total Male Prison Population (Ages 15-70) by State 1940-2010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Morgan Waterman | |
#Lab 5 | |
#Map Animation of Prison Population By State 1940-2010 | |
#load packages | |
library(readr) | |
library(dplyr) | |
library(ggplot2) | |
library(RColorBrewer) | |
library(ggmap) | |
library(maptools) | |
library(gtools) | |
library(devtools) | |
library(gganimate) | |
#read in map dataframe | |
mapdata <-read_csv('data/map.csv') | |
#Create map base | |
map1 <-ggplot() + scale_fill_brewer(palette='Purples') + theme_nothing(legend=TRUE) + | |
geom_polygon(data=mapdata, aes(x=long, y=lat, group=group), fill='white', color='black') | |
png('mapped.png', width=1500, height=1000) | |
print(map1) | |
dev.off() | |
#read in data extract | |
ipums <- read_csv('data/FinalProjectDataNewest.csv', col_types = cols(PERWT=col_double())) | |
#filter for age and exclude Alaska and Hawaii | |
a <- ipums %>% filter((AGE>=15 & AGE<=70) & (!(STATEFIP %in% c(2,15))| YEAR >=1960)) | |
#recode Sex | |
aa <- a %>% mutate(Sex = factor(SEX, labels=c('Male', 'Female'))) | |
#recode Race | |
b <- aa %>% mutate(Race= factor(ifelse(RACE %in% c(2), 1, 2))) | |
labels= c('Black', 'Other') | |
#Recode Institution | |
c <- b %>% mutate(Institution= factor(ifelse(GQTYPE==1, 1, | |
ifelse(GQTYPE==2, 2, 3)))) | |
labels= c('Institution', 'Prison prior to 1990', 'Other Group Quarters') | |
#filter out Other Group Quarters and Female | |
e <- c %>% filter(Institution !='Other Group Quarters') | |
f <- e %>% filter(Sex=='Male') | |
#aggregate data | |
prisonpop <- f %>% group_by(YEAR, STATEFIP) %>% summarise(Number=sum(PERWT)) | |
cuts <- quantcut(prisonpop$Number, q=seq(0,1,.2)) | |
#joining data to map dataframe | |
#change STATEFIP to integer | |
newmap <- mapdata %>% mutate(STATEI=as.integer(STATEFIP)) | |
#create categories by population | |
prisoncats <- prisonpop %>% mutate(Population = factor(ifelse(Number<=277000, 1, | |
ifelse(Number<=679000, 2, | |
ifelse(Number<=1180000, 3, | |
ifelse(Number<=2120000, 4,5)))))) | |
levels(prisoncats$Population) <- c('1-276,999', '277,000-678,999', '679,000-1,179,999', '1,180,000-2,119,999', '2,120,000+') | |
prisonmap <- left_join(prisoncats, newmap, by = c('STATEFIP' = 'STATEI')) %>% arrange(order) | |
prisonmap <- prisonmap %>% arrange(order) | |
#Animating map | |
anmap <- map1 + | |
geom_polygon(data=prisonmap, aes(x=long, y=lat, group=group, fill=Population, frame=YEAR), color='black') + | |
labs(title= 'Total Male Prison Populations (Ages 15-70) by State, ') | |
gg_animate(anmap, ani.width=1500, ani.height=1000, 'anmap.gif') | |
getPalette = colorRampPalette(brewer.pal(9, 'Purples')) | |
map2 <- map1 + scale_fill_manual(values=getPalette(5)) + | |
geom_polygon(data=filter(prisonmap, YEAR==1940), aes(x=long, y=lat, group=group, fill=Population), color= 'black') | |
png('mapped.png', width= 1500, height=1000) | |
print(map2) | |
dev.off() | |
for (year in unique(prisonmap$YEAR)) { | |
map2 <-map1 + theme_bw(base_size=24) + | |
geom_polygon(data=filter(prisonmap, YEAR==year), | |
aes(x=long, y=lat, group=group, fill=Population), color='black') + | |
labs(title=paste('Total Male Prison Populations (Ages 15-70) by State,',year, sep=' ')) | |
png(paste('mapped_', year,'.png', sep=''), width=1500, height=1000) | |
print(map2) | |
dev.off() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment