Skip to content

Instantly share code, notes, and snippets.

@morganmelon
Created October 6, 2016 23:22
Show Gist options
  • Save morganmelon/dc6329990e146cd975051b970d005639 to your computer and use it in GitHub Desktop.
Save morganmelon/dc6329990e146cd975051b970d005639 to your computer and use it in GitHub Desktop.
Population Pyramid for Southeast Asian Immigrants 1880-1960
#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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment