Skip to content

Instantly share code, notes, and snippets.

@r-conway

r-conway/Fig3B.r

Created Oct 24, 2016
Embed
What would you like to do?
#We want to make 2 line graphs, this one for married and one for single with lines for number of children
#load libraries
library(readr)
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(scales)
#load data
a <- read_csv('data/usa_00013.csv',col_types=cols(PERWT=col_double()))
#filter out those under 16 and over 65
b <- a %>% filter(AGE>= 16 & AGE<= 65)
#Remove Alaska and Hawaii
bb <- b %>% filter(YEAR>=1960 | !(STATEFIP %in% c(2,15)))
#assign sex
c <- bb %>%mutate(Sex=factor(SEX, labels=c('Male','Female')))
#Remove Men
cc <- c %>% filter(Sex=='Female')
#Remove married women
d <- cc %>% filter(MARST<3)
#Create factors for number of children
e <- d %>% mutate(Kids=factor(ifelse(NCHILD==0,1,
ifelse(NCHILD==1 | NCHILD==2, 2,
ifelse(NCHILD==3 | NCHILD==4, 3, 4)))))
f <- e %>% mutate(Kids=factor(Kids,labels=c('Zero','One or Two', 'Three or Four','More than Four')))
#Create Occ variable
g <- f %>% mutate(Occ=factor(ifelse(OCC1950>=980,1,2)))
#Split into emploted/unemployed
h <- g %>% mutate(Occ=factor(Occ,labels=c('Not Employed','Employed')))
#Group to calculate denominator of total women single women with each number of kids
i <- h %>% group_by(YEAR,Kids) %>% summarise(Total=sum(PERWT))
#Group to calculate percent employed of single women with each number of kids
j <- h %>% group_by(YEAR,Kids,Occ) %>% summarise(Number=sum(PERWT))
#Join so we can calculate percents
k <- left_join(j,i,by=c('YEAR','Kids'))
#Remove unemployed
l <- k %>% filter(Occ=='Employed')
#Calculate Percentages
m <- l %>% mutate(Percent=Number*100/Total)
#Graph
ggplot(data=m, aes(x=YEAR, y=Percent, group=Kids, colour=Kids)) +
geom_line() +
labs(title='Percent Employment of Married Women by Number of Children from 1920-1970', colour='Number of Children') +
scale_y_continuous(limits=c(0,100), breaks=c(0,25,50,75,100),
labels=c('0%','25%','50%','75%','100%'))
ggsave('Fig3B.pdf',width=10, height=7.5)
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.