Skip to content

Instantly share code, notes, and snippets.

@jailee jailee/Jaicode #3
Created Nov 16, 2016

Embed
What would you like to do?
#Visualization 3: Line graph of white women in different family structure
#load packages
library(dplyr)
library(readr)
library(ggplot2)
library(RColorBrewer)
#Read in IPUMS data;
a <- read_csv('usa_00067.csv',col_types=cols(HHWT=col_double(),PERWT=col_double()))
#Filter women age between 20-40 and exclude Alaska and Hawaii before 1960
b <- a %>% filter(AGE>=20 & AGE<=40 & YEAR>=1960 | !(STATEFIP %in% c(2,15)))
#Label race
c <- b %>% mutate(RACE=factor(ifelse(RACE %in% c(1), 1, 2),
labels=c('white','non-white')))
#Filter white
d <- c %>% filter(RACE=='white')
#Exclude Group Quarters other than households
D <- d %>% filter(GQ==1)
#Label Sex
d1 <- D %>% mutate(SEX=factor(ifelse(SEX==1,1,2)))
levels(d1$SEX)<-c('Male','Female')
#Filter the current data to include only female population
women <- d1 %>% filter(SEX=='Female')
#Label family structure
f <- women %>% mutate(familystructure=factor(ifelse(SPLOC==0 & NCHILD==0,4,
ifelse(SPLOC>0 & NCHILD>0,1,
ifelse(SPLOC>0,2,3))),
labels=c('Married with Childern','Married without Children','Single Parent','Single without Children')))
#Filter income bigger than 0 and smaller than 999999
#Adjust inflation
#SLWT only in 1950
f1 <- f %>% mutate(Inc=as.integer(INCWAGE)) %>% mutate(CP=as.integer(CPI99)) %>%
filter(INCWAGE>0 & INCWAGE<999999) %>%
mutate(IncAdj=Inc*CP) %>%
mutate(WT=as.integer(ifelse(YEAR!=1950,PERWT,SLWT)))
g <- f1 %>% group_by(YEAR,familystructure) %>% summarise(MedInc=median(rep(IncAdj,times=WT)))
Incomeline <- ggplot(data=g,aes(x=YEAR,y=MedInc,group=familystructure,colour=familystructure)) +
geom_line() + geom_point() +
labs(title='Median Income of White Women by Marital Status and Number of children, 1940-2000',x='Year',y='Median Income, US Dollars')
scale_fill_brewer(palette='Set2')
#Export
png('WhiteFamily.png',width=600,height=400)
print(Incomeline)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.