Created
October 17, 2016 16:49
-
-
Save helenaeitel/14709e31e4816c1eb6e899504247ae2f 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
#Helena Eitel | |
#Professor Merchant | |
#QSS 30.05 | |
#Lab Assignment 5 | |
#open packages | |
library(readr) | |
library(dplyr) | |
library(RColorBrewer) | |
library(ggplot2) | |
library(ggmap) | |
library(maptools) | |
library(gtools) | |
#install and open devtools and gganimate | |
install.packages('devtools') | |
library(devtools) | |
devtools::install_github('dgrtwo/gganimate') | |
library(gganimate) | |
#set the working directory to my folder | |
setwd('/Users/heitel/Documents/Dartmouth/16F/QSS 30.5/QSS 30.05 Projects/') | |
#load the blank map file and plot it without data | |
map <- read_csv('./map.csv') | |
map1 <- ggplot() + theme_nothing(legend=TRUE) + | |
geom_polygon(data=map,aes(x=long,y=lat,group=group),fill='white',color='black') | |
png('map1.png',width=1500,height=1000) | |
print(map1) | |
dev.off() | |
#read IPUMS data extract, recode and aggregate data | |
a <- read_csv('./Extract 5.csv',col_types = cols(PERWT=col_double())) | |
b <- a %>% mutate(Sex=factor(SEX,labels=c('Male','Female'))) | |
#look specifically at Japanese Women | |
c <- b %>% filter(RACE==5 & Sex=='Female') | |
jw <- c %>% group_by(YEAR,STATEFIP) %>% summarise(Pop=sum(PERWT)) | |
#STATEFIP is a character in map variable, so create integer version to be able to join to jw | |
newmap <- map %>% mutate(STATEI=as.integer(STATEFIP)) | |
jwmap <- left_join(jw,newmap,by=c('STATEFIP'='STATEI')) %>% arrange(order) | |
#create population categories | |
#look at quintiles for reference | |
cuts <- quantcut(jw$Pop,q=seq(0,1,.2)) | |
jwcats <- jw %>% mutate(Population=factor(ifelse(Pop<200,1, | |
ifelse(Pop<600,2, | |
ifelse(Pop<4000,3, | |
ifelse(Pop<150000,4,5)))))) | |
levels(jwcats$Population) <- c('1-199','200-599','600-3,999','4,000-14,999','15,000+') | |
jwmap2 <- left_join(jwcats,newmap,by=c('STATEFIP'='STATEI')) %>% arrange(order) | |
#map the population (pop category) of Japanese females in 2000 | |
map2 <- map1 + scale_fill_brewer(palette='Blues') + | |
geom_polygon(data=filter(jwmap2,YEAR==2000),aes(x=long,y=lat,group=group,fill=Population),color='black') | |
png('map2.png',width=1500,height=1000) | |
print(map2) | |
dev.off() | |
#OPTION 1: ANIMATE | |
#create an animation of maps of Japanese female population from 1900-2000 | |
#create the code for frames | |
anmap <- map1 + scale_fill_brewer(palette='Blues') + | |
geom_polygon(data=jwmap2,aes(x=long,y=lat,group=group,fill=Population,frame=YEAR),color='black') + | |
labs(title='Population of Japanese Women, ') | |
#animate the frames using gg_animate (with ImageMagick downloaded) | |
gg_animate(anmap,ani.width=1500,ani.height=1000,'anmap.gif') | |
#OPTION 2: STATIC | |
#save a map of the population (pop category) of Japanese females for each year 1900-2000 | |
#for (year in unique(jwmap2$YEAR)) { | |
# map2 <- map1 + scale_fill_brewer(palette='Blues') + theme_bw(base_size = 24) + | |
# geom_polygon(data=filter(jwmap2,YEAR==year),aes(x=long,y=lat,group=group,fill=Population),color='black') + | |
# labs(title=paste('Population of Japanese Women,',year,sep=' ')) | |
# png(paste('map_',year,'.png',sep=''),width=1500,height=1000) | |
# print(map2) | |
# dev.off() | |
#} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment