Last active
November 10, 2016 16:08
-
-
Save morganmelon/83124f1b12d6b5c44829c76b74629ccc to your computer and use it in GitHub Desktop.
Compilation of Final Project Scripts
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 | |
#US History Through Census Data | |
#Final Project: Race, Segregation, and Incarceration in the United States, 1920-2010 | |
#FIGURE 1: Spine Graph of Prison Population by Race 1920-2010 | |
#load packages | |
library(dplyr) | |
library(readr) | |
library(ggplot2) | |
library(RColorBrewer) | |
library(scales) | |
#Program to print plots | |
printplot <- function(plot, filename) { | |
png(paste(filename,'.png', sep=''), height= 500, width = 1000) | |
print(plot) | |
dev.off() | |
} | |
#Read in data extract | |
ipums <- read_csv('data/FINALFINALDATA.csv', col_types = cols(PERWT=col_double())) | |
#Filter for age and exclude Alaska and Hawaii and DC | |
a <- ipums %>% filter(AGE>=15 & AGE<=70) | |
aa<- a %>% filter(STATEFIP!=11 & (!(STATEFIP %in% c(2, 15)) | YEAR >=1960)) | |
#recode RACESING | |
b <- aa %>% mutate(Race = factor(ifelse(RACESING==2, 1, | |
ifelse(RACESING==1, 2, 3)), | |
labels = c('Black', 'White', 'Other'))) | |
#recode GQTYPE--create 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 to get total male prison population | |
e <- c %>% filter(Institution !='Other Group Quarters') | |
f <- e %>% filter(SEX==1) | |
#aggregate data by Year and Race--apply correction factor for 1990 and later | |
prisontotal <- f %>% group_by(YEAR, Race) %>% summarise(NumberTotal=sum(PERWT)) %>% mutate(NumberAdj=ifelse(YEAR>=1990, ifelse(Race=='Black', NumberTotal*0.72, ifelse(Race=='White', NumberTotal*.40, NumberTotal*.58)), NumberTotal)) | |
#Sum | |
prisonrace <- prisontotal %>% group_by(YEAR) %>% summarise(NumberRace=sum(NumberAdj)) | |
#Join so i can calculate percent | |
prisonjoin <- left_join(prisontotal, prisonrace) | |
#Calculate percent | |
prisonpct <- prisonjoin %>% mutate(Percent = NumberAdj/NumberRace) | |
#Graphing | |
graphprison <- ggplot(prisonpct, aes(x= YEAR, y=Percent, fill=Race)) + | |
geom_bar(stat='identity', aes(width=rescale(NumberRace, c(5, 10)))) + | |
labs(fill = 'Race of Prisoners', title = 'Percent of Prison Population by Race, 1920-2010', x = 'Year', y= 'Percent of Prisoners') + | |
theme_bw(base_size=8) + | |
scale_x_continuous(breaks=c(1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010))+ | |
scale_y_continuous(labels=scales::percent) | |
printplot(graphprison, 'FINALspine') | |
#FIGURE 2: Animated Map of Percent of African Americans Males of Total State Male Population 1920-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/FINALFINALDATA.csv', col_types = cols(PERWT=col_double())) | |
#Filter for age and exclude DC & Alaska and Hawaii prior to 1960 | |
a <- ipums %>% filter((AGE>=15 & AGE<=70) & (STATEFIP!=11) & ((!(STATEFIP %in% c(2,15)) | YEAR >=1960))) | |
#recode RACESING | |
b <- a %>% mutate(Race = factor(ifelse(RACESING==2, 1, | |
ifelse(RACESING==1, 2, 3)), | |
labels = c('Black', 'White', 'Other'))) | |
#recode GQTYPE--create Institution | |
#not used in this map | |
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==1) %>% group_by(YEAR,STATEFIP) %>% summarise(TotalAll=sum(PERWT)) | |
#data frame for total black population by state | |
totalblack <- c %>% filter(SEX==1 & Race=='Black') %>% 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) | |
#used to determine "bins" in map | |
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)) | |
#Creating "bins" for percentages | |
totalcats <- blackpct %>% mutate(Percentage = factor(ifelse(Percent<=.01, 1, | |
ifelse(Percent<=.03, 2, | |
ifelse(Percent<=.07, 3, | |
ifelse(Percent<=.15, 4,5)))))) | |
levels(totalcats$Percentage) <- c('0-1%', '1-3%', '3-7%', '7-15%', '15%+') | |
#joining to map | |
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, ') | |
gg_animate(anmap, ani.width=1500, ani.height=1000, 'AfAmofTotalPop.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('mapviz3YAY.png', width= 1500, height=1000) | |
print(map2) | |
dev.off() | |
#for loop to make a map for each year | |
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,',year, sep=' ')) | |
png(paste('mapviz3YAY_', year,'.png', sep=''), width=1500, height=1000) | |
print(map2) | |
dev.off() | |
} | |
#FIGURE 3: Animated Map of Percent of Male African Americans in Total Prison Population By State, 1920-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/FINALFINALDATA.csv', col_types = cols(PERWT=col_double())) | |
#filter for age and exclude DC & Alaska and Hawaii prior to 1960 | |
a <- ipums %>% filter((AGE>=15 & AGE<=70) & (STATEFIP!=11) & ((!(STATEFIP %in% c(2,15)) | YEAR >=1960))) | |
#recode RACESING | |
b <- a %>% mutate(Race = factor(ifelse(RACESING==2, 1, | |
ifelse(RACESING==1, 2, 3)), | |
labels = c('Black', 'White', 'Other'))) | |
#recode GQTYPE--create 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 to get total male prison population | |
e <- c %>% filter(Institution !='Other Group Quarters') | |
f <- e %>% filter(SEX==1) | |
#aggregate data by year, STATEFIP, and Race and apply correction factor for 1990 and after | |
prisontotal <- f %>% group_by(YEAR, STATEFIP, Race) %>% summarise(NumberTotal=sum(PERWT)) %>% mutate(NumberAdj=ifelse(YEAR>=1990, ifelse(Race=='Black', NumberTotal*0.72, ifelse(Race=='White', NumberTotal*.40, NumberTotal*.58)), NumberTotal)) | |
#get total for year | |
prisonyr <- prisontotal %>% group_by(YEAR,STATEFIP) %>% summarise(NumberYear=sum(NumberAdj)) | |
#join so I can calculate percent later | |
prisonpop <- left_join(prisontotal, prisonyr, by = c('YEAR','STATEFIP')) | |
#calculate percent | |
prisonpct <- prisonpop %>% mutate(Percent = NumberAdj/NumberYear) | |
#filter for just black | |
prisonblackpct <- prisonpct %>% filter(Race == 'Black') | |
#used to determine "bins" | |
cuts <- quantcut(prisonblackpct$Percent, q=seq(0,1,.2)) | |
#joining data to map dataframe | |
#change STATEFIP to integer | |
newmap <- mapdata %>% mutate(STATEI=as.integer(STATEFIP)) | |
#create "bins" | |
prisoncats <- prisonblackpct %>% mutate(Percentage = factor(ifelse(Percent<=.10, 1, | |
ifelse(Percent<=.30, 2, | |
ifelse(Percent<=.50, 3, | |
ifelse(Percent<=.70, 4,5)))))) | |
levels(prisoncats$Percentage) <- c('0-10%', '>10-30%', '>30-50%', '>50-70%', '70%+') | |
#joing with map | |
prisonmap <- left_join(prisoncats, newmap, by = c('STATEFIP' = 'STATEI')) %>% 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 of Total Male Prison Population By State, ') | |
gg_animate(anmap, ani.width=1500, ani.height=1000, 'AfAmofTotalPrison.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('mapviz2YAY.png', width= 1500, height=1000) | |
print(map2) | |
dev.off() | |
#for loop for a map of each year | |
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 of Total Male Prison Population By State,',year, sep=' ')) | |
png(paste('mapviz2YAY_', year,'.png', sep=''), width=1500, height=1000) | |
print(map2) | |
dev.off() | |
} | |
#FIGURE 4: Animated Map of Percent of Males in Prison By State, 1920-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/FINALFINALDATA.csv', col_types = cols(PERWT=col_double())) | |
#filter age and exclude Alaska and Hawaii prior to 1960 and DC | |
a <- ipums %>% filter((AGE>=15 & AGE<=70) & (STATEFIP!=11) & ((!(STATEFIP %in% c(2,15)) | YEAR >=1960))) | |
#recode GQTYPE--create Institution | |
c <- a %>% mutate(Institution = factor(ifelse(GQTYPE==1, 1, | |
ifelse(GQTYPE==2, 2, 3)), | |
labels = c('Institution','Prison prior to 1990', 'Other Group Quarters'))) | |
#data frame for total men in state | |
totalmale <- c %>% filter(SEX==1) %>% group_by(YEAR, STATEFIP) %>% summarise(Number=sum(PERWT)) | |
#data frame for men institutionalized in state--apply correction factor for 1990 and after | |
totalprison <- c %>% filter(SEX==1 & Institution!='Other Group Quarters') %>% group_by(YEAR, STATEFIP) %>% summarise(Prison=sum(PERWT)) %>% mutate(NumberAdj=ifelse(YEAR>=1990, Prison *0.5, Prison)) | |
#join | |
totalmaledif <- left_join(totalmale, totalprison) | |
#create percentage variable | |
malepct <- totalmaledif %>% mutate(Percent = NumberAdj/Number) | |
#used to make "bins" | |
cuts <- quantcut(malepct$Percent, q=seq(0, 1, .2)) | |
#joining data to map dataframe | |
newmap <- mapdata %>% mutate(STATEI=as.integer(STATEFIP)) | |
#create "bins" by percent | |
prisoncats <- malepct %>% mutate(Percentage = factor(ifelse(Percent<=.003, 1, | |
ifelse(Percent<=.005, 2, | |
ifelse(Percent<=.007, 3, | |
ifelse(Percent<=.009, 4,5)))))) | |
levels(prisoncats$Percentage) <- c('0-0.3%', '0.3-0.5%', '0.5-0.7%', '0.7-0.9%', '0.9%+') | |
#join to map | |
prisonmap <- left_join(prisoncats, newmap, by = c('STATEFIP' = 'STATEI')) %>% 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 Men in Prison by State, ') | |
gg_animate(anmap, ani.width=1500, ani.height=1000, 'MeninPrison.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=Percentage), color= 'black') | |
png('vizNEW.png', width= 1500, height=1000) | |
print(map2) | |
dev.off() | |
#for loop to make map for each year | |
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 Men in Prison by State,',year, sep=' ')) | |
png(paste('mapvizNEW_', year,'.png', sep=''), width=1500, height=1000) | |
print(map2) | |
dev.off() | |
} | |
#FIGURE 5: Animated Map of Percent of African American men in Prison of Total African American 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/FINALFINALDATA.csv', col_types = cols(PERWT=col_double())) | |
#filter age and exclude Alaska and Hawaii prior to 1960 and DC | |
a <- ipums %>% filter((AGE>=15 & AGE<=70) & (STATEFIP!=11) & ((!(STATEFIP %in% c(2,15)) | YEAR >=1960))) | |
#recode RACESING | |
b <- a %>% mutate(Race = factor(ifelse(RACESING==2, 1, | |
ifelse(RACESING==1, 2, 3)), | |
labels = c('Black', 'White', 'Other'))) | |
#recode GQTYPE--create 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 to get only males | |
e <- c %>% filter(SEX==1) | |
#data frame for total black population by state | |
totalblack <- e %>% filter(Race=='Black') %>% group_by(YEAR, STATEFIP) %>% summarise(TotalBlack=sum(PERWT)) | |
#data frame for black prisoners | |
prisonblack <- e %>% filter(Race=='Black' & Institution!='Other Group Quarters') %>% group_by(YEAR, STATEFIP) %>% summarise(NumberPrison=sum(PERWT)) | |
#apply correction factor for 1990 and after | |
prisonblackcorrect <- prisonblack %>% group_by(YEAR, STATEFIP) %>% mutate(NumberAdj=ifelse(YEAR>=1990, NumberPrison*0.72, NumberPrison)) | |
#join data frames | |
blackdifprison <- left_join(prisonblackcorrect, totalblack, by = c('YEAR','STATEFIP')) | |
#create percentage variable | |
blackpct <- blackdifprison %>% mutate(Percent = NumberAdj/TotalBlack) | |
#used to make "bins" for percent | |
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<=.02, 1, | |
ifelse(Percent<=.03, 2, | |
ifelse(Percent<=.04, 3, | |
ifelse(Percent<=.05, 4,5)))))) | |
levels(prisoncats$Percentage) <- c('0-2%', '2-3%', '3-4%', '4-5%', '5%+') | |
#join to map | |
prisonmap <- left_join(prisoncats, newmap, by = c('STATEFIP' = 'STATEI')) %>% 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 American Males in Prison of Total Afican American Populaiton By State, ') | |
gg_animate(anmap, ani.width=1500, ani.height=1000, 'AnimationStationed.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('FINALviz4.png', width= 1500, height=1000) | |
print(map2) | |
dev.off() | |
#for loop to make map for each year | |
for (year in unique(prisonmap$YEAR)) { | |
map2 <-map1 + theme_bw(base_size=36) + | |
geom_polygon(data=filter(prisonmap, YEAR==year), | |
aes(x=long, y=lat, group=group, fill=Percentage), color='black') + | |
labs(size=36, title=paste('Percent of African American Males in Prison of Total Afican American Populaiton By State,',year, sep=' ')) | |
png(paste('FINALviz4YAY_', year,'.png', sep=''), width=1500, height=1000) | |
print(map2) | |
dev.off() | |
} | |
#FIGURE 6: Map of Segregation Indices 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='Greens') + 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/SegregationIndicesByState.csv') | |
ipums <- ipums %>% arrange(State) | |
#used to make bins | |
cuts <- quantcut(ipums$Index, q=seq(0,1,.2)) | |
#create "bins" for segregation indices | |
segcats <- ipums %>% mutate(Indices = factor(ifelse(Index<=55, 1, | |
ifelse(Index<=59, 2, | |
ifelse(Index<=62, 3, | |
ifelse(Index<=68, 4,5)))))) | |
levels(segcats$Indices) <- c('0-55', '56-59', '60-62', '63-68', '69+') | |
#join to map | |
segmap <- left_join(segcats, mapdata, by = c('State' = 'id')) %>% arrange(order) | |
map2 <- map1 + | |
geom_polygon(data=segmap, aes(x=long, y=lat, group=group, fill=Indices), color= 'black') + | |
labs(title = 'Segregation Indices by State 2005-9') | |
png('mapseg.png', width= 1500, height=1000) | |
print(map2) | |
dev.off() | |
#THE END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment