Skip to content

Instantly share code, notes, and snippets.

@jailee
Created November 16, 2016 19:30
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 jailee/261150e3a40fd0a8fa4b98d5f92c6e50 to your computer and use it in GitHub Desktop.
Save jailee/261150e3a40fd0a8fa4b98d5f92c6e50 to your computer and use it in GitHub Desktop.
#Visualization 4: Column graph for labor division between white men and women
#load packages
library(dplyr)
library(readr)
library(ggplot2)
library(RColorBrewer)
#Read in IPUMS data
a <- read_csv('usa_00067.csv',col_types=cols(PERWT=col_double()))
#Filter age from 15 to 65 and exclude Alaska and Hawaii in the data
b <- a %>% filter(AGE>=15 & AGE<=65 & YEAR>=1960 | !(STATEFIP %in% c(2,15)))
#Label race by character
c <- b %>% mutate(RACE=factor(ifelse(RACE %in% c(1), 1, 2),
labels=c('white','nonwhite')))
#Filter White
d <- c %>% filter(RACE=='white')
#None>=980
#Professional 0~99
#Farmers 100-199, 800-899 (FARMER LABORERS)
#Managerial/clerical/sales 200-499
#Craftsmen/operatives/laborers 500-699 900-979
#Service 700-799
#Create occupation by recoding OCC1950
f <- d %>% mutate(OCCUPATION=factor(ifelse((OCC1950>=100 & OCC1950 <=199)|(OCC1950>=800 & OCC1950<=899), 1,
ifelse(OCC1950>=200 & OCC1950<=299), 2,
ifelse((OCC1950>=599 & OCC1950<=699)|(OCC1950>=900 & OCC1950<=979), 3,
ifelse(OCC1950>=980, 4,5)))),
labels=c('farmers','Managers/Officials/Proprietors','craftmen/operatives/laborers','none','other'))
#Label Sex
o3 <- f %>% mutate(SEX=ifelse(SEX==1,'male','female'))
#Group by Year, Race, Sex, and Occupation and weight by PERWT
o4 <- o3 %>% group_by(YEAR,RACE,SEX,OCCUPATION) %>% summarise(Number=sum(PERWT))
#Graphing Figure 2
png('OccupM-F.png',height=500,width=1000)
fig2 <- ggplot(data=o4,aes(x=YEAR,y=Number,fill=OCCUPATION))+
labs(x='Year',y='Percent of Population',fill='Occupation',title='Occupation of White Americans by Sex and Year, 1940-2000')+
geom_bar(stat='identity', position='fill') +
scale_y_continuous(labels=scales::percent) +
scale_x_continuous(breaks=c(1940,1970,2000)) +
scale_fill_brewer(palette='Set1') +
facet_grid(SEX~.~RACE) +
theme_bw() + theme(legend.position='bottom')
#Export figures to png
png('Occup.png',height=500,width=1000)
print(fig2)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment