Skip to content

Instantly share code, notes, and snippets.

@r-conway

r-conway/fig2A.r

Last active Oct 24, 2016
Embed
What would you like to do?
#line graph for race for single women
#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
d <- c %>% filter(Sex=='Female')
#Remove married women
e <- d %>% filter(MARST>=3)
#Create Race catagories
f <- e %>% mutate(Race=factor(ifelse(RACE==1,1,
ifelse(RACE==2,2,
ifelse(RACE==3,3,4)))))
g <- f %>% mutate(Race=factor(Race,labels=c('White','Black','Native American','Asian')))
#Creat Occ avriable
h <- g %>% mutate(Occ=factor(ifelse(OCC1950>=980,1,2)))
#Split into employed/unemployed
i <- h %>% mutate(Occ=factor(Occ,labels=c('Not Employed','Employed')))
#Group to calculate total single women of each race for each year
j <- i %>% group_by(YEAR,Race) %>% summarise(Total=sum(PERWT))
#group to calculate total percent employed of single women for each year
k <- i %>% group_by(YEAR,Race,Occ) %>% summarise(Number=sum(PERWT))
#link so we can calculate percent
l <- left_join(k,j,by=c('YEAR','Race'))
#Remove unemployed
m <- l %>% filter(Occ=='Employed')
#Calculate percentages
n <- m %>% mutate(Percent=Number*100/Total)
#Graph
ggplot(data=n, aes(x=YEAR, y=Percent, group=Race, colour=Race)) +
geom_line() +
labs(title='Percent Employment of Single Women by Race from 1920-1970',x='Year', colour='Race of Woman') +
scale_y_continuous(limits=c(0,100), breaks=c(0,25,50,75,100),
labels=c('0%','25%','50%','75%','100%')) +
ggsave('Fig2.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.