Created
October 16, 2016 21:40
-
-
Save morganmelon/3c6b867f99c1a477b2ea16939ecca558 to your computer and use it in GitHub Desktop.
Map Animation of Percent of African Americans Who are Incarcerated 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 African Americans Who are Incarcerated 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='Blues') + 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 prior to 1960 | |
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 Female | |
e <- c %>% filter(Sex=='Male') | |
#data frame for total black population by state | |
totalblack <- e %>% filter(Race==1) %>% group_by(YEAR, STATEFIP) %>% summarise(TotalBlack=sum(PERWT)) | |
#data frame for black prisoners | |
prisonblack <- e %>% filter(Race==1 & Institution!=3) %>% group_by(YEAR,STATEFIP) %>% summarise(PrisonBlack=sum(PERWT)) | |
#join data frames | |
blackpop <- left_join(prisonblack, totalblack, by = c('YEAR','STATEFIP')) | |
#create percentage variable | |
blackpct <- blackpop %>% mutate(Percent = PrisonBlack/TotalBlack) | |
cuts <- quantcut(blackpct$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 <- blackpct %>% mutate(Percentage = factor(ifelse(Percent<=.0192, 1, | |
ifelse(Percent<=.0274, 2, | |
ifelse(Percent<=.0522, 3, | |
ifelse(Percent<=.0761, 4,5)))))) | |
levels(prisoncats$Percentage) <- c('0-1.92%', '1.92-2.74%', '2.74-5.22%', '5.22-7.61%', '7.61%+') | |
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 African Americans (Ages 15-70) Who are Incarcerated By State 1940-2010, ') | |
gg_animate(anmap, ani.width=1500, ani.height=1000, 'anmapraced.gif') | |
getPalette = colorRampPalette(brewer.pal(9, 'Blues')) | |
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('mapraced.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