Last active
October 20, 2016 17:45
-
-
Save morganmelon/fa9cce33e43da54208786f11c4c29104 to your computer and use it in GitHub Desktop.
Percent of African American Males of Total State Male Population 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 Males of Total State Male Population (Ages 15-70) 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='Oranges') + 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 DC and Alaska and Hawaii prior to 1960 | |
a <- ipums %>% filter((AGE>=15 & AGE<=70) & (STATEFIP !=11) & (!(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(1), 1, | |
ifelse(RACE %in% c(2), 2, 3)))) | |
labels= c('White', '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 for total male population | |
totalall<- c %>% filter(Sex=='Male') %>% group_by(YEAR,STATEFIP) %>% summarise(TotalAll=sum(PERWT)) | |
#data frame for total black population by state | |
totalblack <- c %>% filter(Sex=='Male' & Race==2) %>% group_by(YEAR, STATEFIP) %>% summarise(TotalBlack=sum(PERWT)) | |
#join data frames | |
totalpop <- left_join(totalall, totalblack, by = c('YEAR','STATEFIP')) | |
#create percentage variable | |
blackpct <- totalpop %>% mutate(Percent = TotalBlack/TotalAll) | |
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 | |
totalcats <- blackpct %>% mutate(Percentage = factor(ifelse(Percent<=.0104, 1, | |
ifelse(Percent<=.0368, 2, | |
ifelse(Percent<=.0746, 3, | |
ifelse(Percent<=.151, 4,5)))))) | |
levels(prisoncats$Percentage) <- c('0-1.04%', '1.04-3.68%', '3.68-7.46%', '7.46-15.1%', '15.1%+') | |
prisonmap <- left_join(totalcats, 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 Males of Total State Male Population (Ages 15-70), ') | |
gg_animate(anmap, ani.width=1500, ani.height=1000, 'anmapraced2.gif') | |
getPalette = colorRampPalette(brewer.pal(9, 'Oranges')) | |
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('mapblack.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 African Americans Males of Total State Male Population (Ages 15-70),',year, sep=' ')) | |
png(paste('mapblack_', 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