Skip to content

Instantly share code, notes, and snippets.

/Occ

Created Nov 14, 2016
Embed
What would you like to do?
FP - Occ
#open libraries
library(ggplot2)
library(dplyr)
library(readr)
library(RColorBrewer)
#read csv
csv <- read_csv('~/Desktopusa_00028.csv')
#only women over 22
women <- csv_new %>% filter(SEX==2 & AGE>=22)
# Create Occupation Variable
Occupations <- women %>% mutate(Occupation=factor(ifelse(OCC1950>=979 & OCC1950<=999,1,
ifelse(OCC1950>=100 & OCC1950<=123 | OCC1950>=810 & OCC1950<=840, 2,
ifelse(OCC1950>=500 & OCC1950<=690 | OCC1950>=910 & OCC1950<=970,3,
ifelse(OCC1950>=200 & OCC1950<=490,4,
ifelse(OCC1950>=700 & OCC1950<=790,5,
ifelse(OCC1950>=000 & OCC1950<=99,6,0)))))),
labels=c('None','Farmers and Farm Laborers','Craftsmen/Operatives/Laborers',
'Managerial/Clerical/Sales','Service',
'Professional')))
#recode sex
sex <- Occupations %>% mutate(Sex=ifelse(SEX==2,'female'))
#group race
race_grouped <- sex %>% mutate(Race=factor(ifelse(HISPAN>0,1,
ifelse(RACESING==1,2,
ifelse(RACESING==2,3,
ifelse(RACESING==3,4,
ifelse(RACESING==4,5,6)))))))
levels(race_grouped$Race) <- c('Hispanic','White','Black','Native American','Asian','Other')
#select only needed variables
select <- race_grouped %>% select(YEAR,PERWT,Occupation,Sex, Race)
#make graph groupings - Year, Sex, Region, and Occupation
group_2 <- select %>% group_by(YEAR, Sex, Race, Occupation) %>% summarise(Number=sum(PERWT))
#graphing figure 4
ggplot(data=group_2,aes(x=YEAR, y=Number, fill=Occupation)) +
geom_bar(stat='identity', position='fill') +
labs(x='Year',y='Percent of Population',fill='Occupation', title='Occupation of Women Over 20 by Race and Year, 1940-2000') +
scale_y_continuous(labels=scales::percent) +
scale_x_continuous(breaks=c(1940,1960,1980,2000)) +
scale_fill_brewer(palette='Set1') +
facet_grid(~Race) +
theme(legend.position='bottom')
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.