Created
October 23, 2016 00:03
-
-
Save morganmelon/1090875566ccacf3b08df38026b81daa to your computer and use it in GitHub Desktop.
Visualization 4: Spine Graph showing Percent of Prison Population By Race, 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 | |
#US History Through Census Data | |
#Spine Graph for African American Prison Populations | |
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/FinalProjectDataNewest.csv', col_types = cols(PERWT=col_double())) | |
#Filter for age and exclude Alaska and Hawaii | |
a <- ipums %>% filter(AGE>=15 & AGE<=70) | |
aa<- a %>% filter(!(STATEFIP %in% c(2, 15)) | YEAR >=1960) | |
#recode RACE | |
b <- aa %>% mutate(Race = factor(ifelse(RACE %in% c(2), 1, 2), | |
labels = c('Black', '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 Group Quarters and females | |
prisontotal <- c %>% filter(Institution!=3 & SEX==1) | |
#data frame just blacks | |
prisonblack <- prisontotal %>% filter(Race==2) %>% select(YEAR, Race) %>% rename(Brace=Race) | |
prisonpop <- left_join(prisontotal, prisonblack) %>% group_by(YEAR, Race) %>% summarise(Number=sum(PERWT)) | |
#make total for yr | |
prisonyr <- prisonpop %>% group_by(YEAR) %>% mutate(totalprison=sum(Number)) | |
prisonjoin <- left_join(prisonpop, prisonyr) | |
prisonpct <- prisonjoin %>% mutate(Percent = Number/totalprison) | |
#Prison spine graph | |
graphprison <- ggplot(prisonpct, aes(x= YEAR, y=Percent, fill=Race)) + | |
geom_bar(stat='identity') + | |
labs(fill = 'Race of Prisoners', title = 'Percent of Prisoners Who Are African American, 1940-2010', x = 'Year', y= 'Percent of Prisoners') + | |
theme_bw(base_size=8) + | |
scale_x_continuous(breaks=c(1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010)) | |
scale_y_continuous(labels=scales::percent) | |
printplot(graphprison, 'prisonrace') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment