Last active
October 16, 2016 22:15
-
-
Save morganmelon/8f3c006ad110ad351fd5a9494108efcb to your computer and use it in GitHub Desktop.
Percent of Male African Americans in Total 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 Percent of Male African Americans in Total Prison Population (Ages 15-70) 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='Reds') + theme_nothing(legend=TRUE) + | |
geom_polygon(data=mapdata, aes(x=long, y=lat, group=group), fill='white', color='black') | |
png('maprace.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 Institution prior to 1990 and Female | |
e <- c %>% filter(Institution !='Other Group Quarters') | |
f <- e %>% filter(Sex=='Male') | |
#aggregate data | |
prisontotal <- f %>% group_by(YEAR, STATEFIP) %>% summarise(NumberTotal=sum(PERWT)) | |
prisonblack <- f %>% filter(Race==1) %>% group_by(YEAR,STATEFIP) %>% summarise(NumberBlack=sum(PERWT)) | |
prisonpop <- left_join(prisontotal, prisonblack, by = c('YEAR','STATEFIP')) | |
prisonpct <- prisonpop %>% mutate(Percent = NumberBlack/NumberTotal) | |
cuts <- quantcut(prisonpct$Percent, 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 <- prisonpct %>% mutate(Percentage = factor(ifelse(Percent<=.0107, 1, | |
ifelse(Percent<=.0372, 2, | |
ifelse(Percent<=.0759, 3, | |
ifelse(Percent<=.159, 4,5)))))) | |
levels(prisoncats$Percentage) <- c('0-1.07%', '1.07-3.72%', '3.72-7.59%', '7.59-15.9%', '15.9%+') | |
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=Percentage, frame=YEAR), color='black') + | |
labs(title= 'Percent of Male African Americans in Total Prison Population (Ages 15-70) By State, ') | |
gg_animate(anmap, ani.width=1500, ani.height=1000, 'anmaprace.gif') | |
getPalette = colorRampPalette(brewer.pal(9, 'Reds')) | |
map2 <- map1 + scale_fill_manual(values=getPalette(5)) + | |
geom_polygon(data=filter(prisonmap, YEAR==1940), aes(x=long, y=lat, group=group, fill=Percentage), color= 'black') | |
png('maprace.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=Percentage), color='black') + | |
labs(title=paste('Percent of Male African Americans in Total Prison Population (Ages 15-70) By State,',year, sep=' ')) | |
png(paste('maprace_', 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