Created
September 30, 2016 19:07
-
-
Save r-conway/7669997003e9d08369f50b5f1afda23c 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
library(dplyr) | |
library(readr) | |
library(ggplot2) | |
library(RColorBrewer) | |
#read in data | |
a<- read_csv('./data/usa_00005.csv') | |
#remove Hawaii and Alaska and pick out those of working age | |
b<- a %>% filter(AGE>=15 & AGE<=65 & !(STATEFIP %in% c(2,15))) | |
#Creating Race Groups | |
c <- b %>% mutate(RACEGROUP=factor(ifelse((RACE==1),1, | |
ifelse((RACE==2),2, | |
ifelse((RACE==3),3, | |
4))), | |
labels=c('White','Black','Native American', 'Asian'))) | |
#Recoding sex | |
d <- c %>% mutate(Sex=ifelse(SEX==1, 'male', 'female')) | |
#Creating Occupation catagory | |
e <- d %>% mutate(OCCUPATION=factor(ifelse(OCC1950<100, 6, | |
ifelse(OCC1950>=980, 1, | |
ifelse((OCC1950>=500 & OCC1950<=690) | (OCC1950>=910 & OCC1950<=970), 3, | |
ifelse(OCC1950>=700 & OCC1950<=790, 5, | |
ifelse(OCC1950>=200 & OCC1950<=490, 4,2))))), | |
labels=c('None','Farmers/Farm Laborers','Craftsmen/Operatives/Laborers','Managerial/Clerical/Sales','Service','Professionals'))) | |
#Select just the variables I need | |
f <- e %>% select(YEAR, PERWT, Sex, RACEGROUP, OCCUPATION) | |
#For figrure 2 | |
f1 <- f %>% group_by(YEAR, Sex, RACEGROUP) %>% summarise(NUMBER=sum(PERWT)) | |
#For figure 4 | |
f2 <- f %>% group_by(YEAR, Sex, RACEGROUP, OCCUPATION) %>% summarise(NUMBER=sum(PERWT)) | |
#GRAPHING | |
#Create preliminary graph of figure 2 | |
ggplot(data=f1, aes(x=YEAR, y=NUMBER, fill=Sex)) + | |
geom_bar(stat='identity') + | |
labs(x='Year', y='Population', fill='Sex', title='2. Population Aged 15-65 by Race, Year, and Sex, 1870-1920')+ | |
scale_y_continuous(labels=scales::comma) + | |
scale_x_continuous(breaks=c(1870,1900,1920)) + | |
scale_fill_brewer(palette='Set2',guide=guide_legend(reverse=TRUE)) + | |
facet_wrap(~RACEGROUP,ncol=2,scales='free_y') + | |
theme_bw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment