Skip to content

Instantly share code, notes, and snippets.

@r-conway
Last active October 24, 2016 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-conway/bf4672dc53e4d916ba8ab2f6d27401c0 to your computer and use it in GitHub Desktop.
Save r-conway/bf4672dc53e4d916ba8ab2f6d27401c0 to your computer and use it in GitHub Desktop.
#We want to make 2 line graphs, one for married and this 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 Single Women by Number of Children from 1920-1970', x='Year', 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('Fig3A.pdf',width=10, height=7.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment