Created
October 24, 2016 18:00
-
-
Save r-conway/ee35ef91c41867523bae10688f25a2ab to your computer and use it in GitHub Desktop.
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
#load libraries | |
library(readr) | |
library(dplyr) | |
library(ggplot2) | |
library(RColorBrewer) | |
library(scales) | |
#load data | |
ipums <- read_csv('data/usa_00011.csv', col_types=cols(HHWT=col_double(),PERWT=col_double())) | |
#Remove Alaska and Hawaii | |
a <- ipums %>% filter(!(YEAR < 1960 & STATEFIP %in% c(2,15))) | |
#Make factors for region and sex | |
b <- a %>% mutate(REGION=factor(ifelse(STATEFIP %in% c(17, 18, 19, 20, 26, 27, 29, 31, 38, 39, 46, 55), 1, | |
ifelse(STATEFIP %in% c(4, 6, 8, 16, 30, 32, 35, 41, 49, 53, 56), 2, | |
ifelse(STATEFIP %in% c(9, 23, 25, 33, 34, 36, 42, 44, 50), 3, 4))), | |
labels=c('Midwest','West','Northeast','South'))) | |
#Get rid of group quarters | |
bb<- b %>% filter(GQ==1) | |
#Make data set of children | |
c <- bb %>% filter(AGE<18) | |
#Make data set of adult men | |
d <- a %>% filter(SEX==1 & AGE>=18) | |
dd <- d %>% mutate(DRace=RACE) | |
ddd <- dd %>% select(YEAR,SERIAL,DRace,PERNUM) | |
#Attach them if poploc=pernum (if they're dads) | |
e <- left_join(c, ddd, by=c('YEAR','SERIAL','POPLOC'='PERNUM')) | |
#make data set of adult women | |
g <- a %>% filter(SEX==2 & AGE >=18) | |
gg <- g %>% mutate(MRace=RACE) | |
ggg <- gg %>% select(YEAR,SERIAL,MRace,PERNUM) | |
#Attach moms to children and dads | |
h <- left_join(e,ggg, by=c('YEAR','SERIAL','MOMLOC'='PERNUM')) | |
#Sort into catagories | |
i <- h %>% mutate(Type=factor(ifelse(MOMLOC==0 | POPLOC==0,1, | |
ifelse(MRace!=DRace,2,3)), | |
labels=c('Only one Parent','Different Races', 'Same Race'))) | |
#Total up by region and year to get number of children | |
j <- i %>% group_by(YEAR,REGION) %>% summarize(Total=sum(PERWT)) | |
#Total up each type of household | |
k <- i %>% group_by(YEAR,REGION,Type) %>% summarize(Number=sum(PERWT)) | |
#Join the matricies so they can be divided | |
l <- left_join(k,j, by=c('YEAR','REGION')) | |
#Divide them to get percentages | |
m <- l %>% mutate(Percent=Number/Total) | |
#Create Spine graph: | |
graph1 <- ggplot(m,aes(x=REGION, y=Percent, fill=Type)) + | |
geom_bar(aes(width=rescale(Total,c(.1,1))), stat='identity', position='stack') + | |
scale_y_continuous(labels=scales::percent) + | |
scale_fill_brewer(palette='Set2') + | |
facet_wrap(~YEAR, ncol = 5)+ | |
labs(title = 'Percent of Children with Parents of Same Race and Different Races, 1900-1990', x = 'Region', y = 'Percent of Children', fill = 'Parents') | |
#Label the Different Race Percent | |
graph2 <- graph1 + | |
geom_text(label=ifelse(m$Type=='Different Races',paste(round(m$Percent*100,1),'%',sep=''),''), | |
y=ifelse(m$Type=='Different Races',.5,.9), angle=90) | |
ggsave('TitleKids.pdf',width=20, height=7.5) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment