Created
October 7, 2016 00:53
-
-
Save morganmelon/7e8640198245aa468fab365542143587 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
#Morgan Waterman | |
#Lab 4 | |
#Population Pyramids | |
#Load packages | |
library(readr) | |
library(dplyr) | |
library(ggplot2) | |
library(RColorBrewer) | |
#Read in IPUMS | |
a <- read_csv('desktop/IMPT_GIT/data/Lab4Data.csv') | |
#Create vector of 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 sex, age, generation | |
b <- a %>% mutate(Sex = factor(SEX, labels=c('Male', 'Female'))) | |
c <- b %>% mutate(Age= ifelse(AGE >= 80, 8, floor(AGE/10))) | |
d <- c %>% mutate(Age= factor(Age, labels=agecats)) | |
e <- d %>% mutate(Gen = ifelse(BPL >= 510 & BPL<=519, 'First Generation', | |
ifelse((MBPL >= 510 & MBPL <= 519) | (FBPL >= 510 & FBPL <= 519), 'Second Generation', | |
'Neither'))) | |
#exclude Alaska and Hawaii | |
f <- e %>% filter(YEAR >=1960 | !(STATEFIP %in% c(2, 15))) | |
#filter for generations | |
g <- f %>% filter(Gen != 'Neither') | |
#aggregate data | |
g2 <- g %>% mutate(Weight = ifelse(YEAR==1940 & Gen=='Second Generation', | |
SLWT, PERWT)) | |
h <- g2 %>% group_by(Age, Sex, Gen, YEAR) %>% summarise(Number=sum(Weight)) | |
#Make male population negative for visualization purposes | |
h2 <- h %>% mutate(Number = ifelse(Sex=='Male', -1 *Number, Number)) | |
#make and export population pyramid | |
png('Population_Southeast_Asia.png', height = 500, width = 2000) | |
ggplot(data = h2, aes(x=Age, y=Number, fill=Sex)) + | |
geom_bar(data = h2[h2$Sex=='Male',], stat = 'identity') + | |
geom_bar(data = h2[h2$Sex=='Female',], stat = 'identity') + | |
coord_flip() + | |
facet_grid(Gen~.~YEAR) + | |
scale_y_continuous(breaks = c(-25000, -12500, 0, 125000, 25000), | |
labels = c('25', '12.5', '0', '12.5', '25')) + | |
labs(fill= '', y='Population (Thousands)', title = 'Population Pyramids for Southeast Asian Immigrants and Their Children') + | |
scale_fill_brewer(palette='Set1', guide=guide_legend(reverse=TRUE)) + | |
theme_bw() + theme(legend.position= 'bottom') | |
dev.off() | |
h3 <- h2[h2$YEAR != 1940 & h2$YEAR != 1960,] | |
png('Population_Southeast_Asia_2.png', height = 500, width = 2000) | |
ggplot(data = h3, aes(x=Age, y=Number, fill=Sex)) + | |
geom_bar(data = h3[h3$Sex=='Male',], stat = 'identity') + | |
geom_bar(data = h3[h3$Sex=='Female',], stat = 'identity') + | |
coord_flip() + | |
facet_grid(Gen~.~YEAR) + | |
scale_y_continuous(breaks = c(-15000, -7500, 0, 7500, 15000), | |
labels = c('25', '12.5', '0', '12.5', '25')) + | |
labs(fill= '', y='Population (Thousands)', title = 'Population Pyramids for Southeast Asian Immigrants and Their Children') + | |
scale_fill_brewer(palette='Set1', guide=guide_legend(reverse=TRUE)) + | |
theme_bw() + theme(legend.position= 'bottom') | |
dev.off() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment