Last active
November 15, 2016 17:02
-
-
Save r-conway/2b4d42fe0b20fd665009d2b162910c33 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 | |
a <- read_csv('data/usa_00013(1).csv',col_types=cols(PERWT=col_double())) | |
#filter out those under 16 and over 65 | |
b <- a %>% filter(AGE>= 16 & AGE<= 65) | |
#assign sex | |
c <- b %>%mutate(Sex=factor(SEX, labels=c('Male','Female'))) | |
#Remove Men | |
cc <- c %>% filter(Sex=='Female') | |
#Create age groupings******************************* | |
d <- cc %>% mutate(Agegroup=(factor(ifelse(AGE<=25,1, | |
ifelse(AGE<=35,2, | |
ifelse(AGE<=45,3, | |
ifelse(AGE<=55,4,5))))))) | |
#label age groupings | |
e <- d %>% mutate(Agegroup=factor(Agegroup,labels=c('16-25','26-35', | |
'36-45','46-55','56-65'))) | |
#remove Alaska and Hawaii | |
f <- e %>% filter(YEAR>=1960 | !(STATEFIP %in% c(2,15))) | |
#Desrcibe employment: | |
g <- f %>% mutate(Occ=factor(ifelse(OCC1950>=980,1,2))) | |
#Label employment: | |
h <- g %>% mutate(Occ=factor(Occ,labels=c('Not Employed','Employed'))) | |
#Create Marriage groupings | |
i <- h %>% mutate(Status=factor(ifelse(MARST<=2,1,2))) | |
#label Marriage groupings | |
j <- i %>% mutate(Status=factor(Status,labels=c('Married','Single'))) | |
#group to calculate totals of women married/single in each year | |
k <- j %>% group_by(YEAR,Status,Agegroup) %>% summarize(Total=sum(PERWT)) | |
#group to calculate totals of employed married/single women in each year | |
l <- j %>% group_by(YEAR,Status,Occ,Agegroup) %>% summarize(Number=sum(PERWT)) | |
#join them so we can divide them | |
m <- left_join(l,k, by=c('YEAR','Status','Agegroup')) | |
#Remove the unemployed so its easier to see percentages | |
n <- m %>% filter(Occ=='Employed') | |
#Divide to get percents | |
o <- n %>% mutate(Percent=Number*100/Total) | |
#Negate single women so that they graph opposite the axis | |
p <- o %>% mutate(Percent=ifelse(Status=='Single',-1*Percent,Percent)) | |
#Graph | |
ggplot(data=p,aes(x=Agegroup,y=Percent,fill=Status)) + | |
geom_bar(data=p[p$Status=='Single',],stat='identity') + | |
geom_bar(data=p[p$Status=='Married',],stat='identity') + | |
coord_flip() + | |
facet_grid(~YEAR) + | |
scale_y_continuous(limits=c(-100,100), breaks=c(-50,0,50), | |
labels=c('50%','0%','50%')) + | |
labs(y='Percent Employed',title='Percent Employment among Married and Single Women from 1920-1970',x='Age') + | |
scale_fill_brewer(palette='Set1',guide=guide_legend(reverse=TRUE)) + | |
theme(legend.position='bottom')+ | |
theme_bw() | |
ggsave('Fig1.pdf',width=10, height=7.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment