Created
October 10, 2016 00:05
-
-
Save lfortin-117/4a943f27a5260faedd3c6fc224b74d1a to your computer and use it in GitHub Desktop.
Lab 4 - LF
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(ggplot2) | |
library(readr) | |
library(RColorBrewer) | |
#Read in the IPUMS data | |
x <- read_csv('LAB4.csv') %>% filter((BPL>=500 & BPL<=502) | (MBPL>=500 & MBPL<=502) | (FBPL>=500 & FBPL<=502)) | |
#Create a vector for age category labels | |
agecats <- '0-9' | |
for (i in 1:7) { | |
agecats <- c(agecats, paste(i, '0-', i, 9, sep='')) | |
} | |
agecats <- c(agecats,'80+') | |
#Recode for gender and age | |
a <- x %>% mutate(Sex=factor(SEX,labels=c('Male','Female'))) | |
b <- a %>% mutate(Age=ifelse(AGE>=80,8, floor(AGE/10))) | |
#Get it into a factor with labels, as we only have numeric now | |
c <- b %>% mutate(Age=factor(Age, labels=c(agecats))) | |
#First or second generation | |
d <- c %>% mutate(Gen=ifelse(BPL>99, 'First Generation', | |
ifelse(MBPL>99 | FBPL>99, 'Second Generation', | |
'Neither'))) | |
#Sorting out Alaska and Hawaii | |
e <- d %>% filter(YEAR>=1960 | !(STATEFIP %in% c(2,15))) | |
#Singling first or second generation | |
f <- e %>% filter(Gen != 'Neither') | |
#SLWT for those in 1940 Second Generation | |
g1 <- f %>% mutate(Weight= ifelse(YEAR==1940 & Gen=='Second Generation',SLWT,PERWT)) | |
#Aggregating | |
g <- g1 %>% group_by(Age, Sex, Gen, YEAR) %>% summarise(Number=sum(Weight)) | |
H <- g %>% mutate(Number=ifelse(Sex=='Male',-1 *Number, Number)) | |
#Graphing | |
ggplot(data=H, aes(x=Age, y=Number, fill=Sex)) + | |
geom_bar(data=H[H$Sex=='Male',],stat='identity') + | |
geom_bar(data=H[H$Sex=='Female',],stat='identity') + | |
coord_flip() + | |
facet_grid(Gen~.~YEAR) + | |
scale_y_continuous(breaks = c(-50000,-25000,0,25000,50000), | |
labels = c('5','2.5','0','2.5','5')) + | |
labs(y='Population in Tens of Thousands', title= 'Population Pyramids for East Asian Immigrants and their Children') + | |
scale_fill_brewer(palette='Set1', guide=guide_legend(reverse=TRUE))+ | |
theme(legend.position='bottom') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment