Created
October 24, 2016 18:01
-
-
Save r-conway/9295952ce29d7a235240fd62e6e2c9fc 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_00010.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'))) | |
c <- b %>% mutate(SEX==1, 'male', 'female') | |
#Remove group quarters | |
d<- c %>% filter(GQ==1) | |
#Create data set for head of house | |
e <- d %>% filter(RELATE==1) | |
f <- e %>% mutate(HRace=RACE) | |
#Create data set for spouses of head of house | |
g <- d %>% filter(RELATE==2) | |
h <- g %>% mutate(SRace=RACE) | |
i <- h %>% select(YEAR, SERIAL,PERNUM, SRace) | |
#Merge data sets to make coupling | |
j <- left_join(f,i, by=c('YEAR','SERIAL','SPLOC'='PERNUM')) | |
#Group into | |
k <- j %>% mutate(Status=factor(ifelse(SPLOC==0,1, | |
ifelse(HRace!=SRace, 2, 3)), | |
labels=c('Head Unmarried', 'Different Races', 'Same Race'))) | |
#Total up by region and year to get total households | |
l <- k %>% group_by(YEAR,REGION) %>% summarize(Number=sum(HHWT)) | |
#Group by type of household | |
m <- k%>% group_by(YEAR,REGION,Status) %>% summarize(Total=sum(HHWT)) | |
#Join them so they can be divided | |
n <- left_join(m,l,by=c('YEAR','REGION')) | |
#Divide to get percent of each type | |
o <- n %>% mutate(Percent=Total/Number) | |
#Create Spine Graph | |
graph1 <- ggplot(o,aes(x=REGION, y=Percent, fill=Status)) + | |
geom_bar(aes(width=rescale(Number, c(.1,1))), stat='identity') + | |
scale_y_continuous(labels=scales::percent) + | |
scale_fill_brewer(palette='Set2') + | |
facet_wrap(~YEAR, ncol = 5)+ | |
labs(title = 'Percent of Households Headed by Couples of Same Race and Different Races, 1900-1990', x = 'Region', y = 'Percent of Household', fill = 'Head and Spouse') | |
graph2 <- graph1 + | |
geom_text(label=ifelse(o$Status=='Different Races',paste('Different ',round(o$Percent*100,1),'%',sep=''),''), | |
y=ifelse(o$Status=='Different Races',.5,.9),angle=90) | |
ggsave('PercentCouples.pdf',width=20, height=7.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment