Skip to content

Instantly share code, notes, and snippets.

@r-conway
Last active October 17, 2016 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-conway/08afe3e3b06e6b060a012a3daf1546b6 to your computer and use it in GitHub Desktop.
Save r-conway/08afe3e3b06e6b060a012a3daf1546b6 to your computer and use it in GitHub Desktop.
#load library
library(readr)
library(dplyr)
library(ggplot)
library(RColorBrewer)
library(ggmap)
library(maptools)
library(gtools)
#Clear Enviornment
rm(list=ls())
#load map data
mapdata <- read_csv('data/map.csv')
#load blank map
map1<- ggplot() +theme_nothing(legend=TRUE)
geom_polygon(data=mapdata, aes(x=long, y=lat, group=group), fill='white', color='black')
png('map.png', width=1500,height=1000)
print(map1)
dev.off()
#load ipums data
ipums=read_csv('data/usa_00007.csv',col_types = cols(PERWT=col_double()))
#filter to just white women
women <- ipums %>% filter(SEX==2)
whwomen <- women %>% filter(RACE==1)
#Remove alaska and Hawaii
whwomen1<- whwomen %>%filter(YEAR>=1960 | !(STATEFIP %in% c(2,15)))
#group women by state and add up populations
ds <- whwomen1 %>% group_by(YEAR,STATEFIP) %>% summarise(Number=sum(PERWT))
#create map with integer values to make it compatible
newmap <- mapdata %>% mutate(STATEI=as.integer(STATEFIP))
#combine map with ipums data
dsmap <- left_join(ds,newmap,by=c('STATEFIP'='STATEI'))
#plot map
map2<- ggplot() + theme_nothing(legend=TRUE) +
geom_polygon(data=mapdata, aes(x=long,y=lat,group=group),fill='white',color='black')
geom_polygon(data=filter(dsmap, YEAR==1900),aes(x=long,y=lat,group=group,fill=Number), color='black')
png('map.png',width=1500,height=1000)
print(map2)
dev.off()
#find roughly even 5ths of data and group the data into them
cuts<-quantcut(ds$Number,q=seq(0,1,.2))
dscats <- ds %>% mutate(Population=factor(ifelse(Number<250000,1,
ifelse(Number<600000,2,
ifelse(Number<1000000,3,
ifelse(Number<2000000,4,5))))))
levels(dscats$Population) <- c('1-250000','250000-600000','600000-1000000','1000000-2000000','2000000+')
#compine map with ipums
dsmap <- left_join(dscats,newmap,by=c('STATEFIP'='STATEI')) %>% arrange(order)
#format map
map2<- map1 +scale_fill_brewer(palette='Blues') +
geom_polygon(data=filter(dsmap,YEAR==1900), aes(x=long,y=lat,group=group,fill=Population),color='black')
png('map.png',width=1500,height=1000)
print(map2)
dev.off()
#create a loop to make the same map for all the relevant years
for(year in unique(dsmap$YEAR)) {
map2<- map1 +scale_fill_brewer(palette='Blues') +
geom_polygon(data=filter(dsmap,YEAR==year), aes(x=long,y=lat,group=group,fill=Population),color='black') +
labs(title=paste('White Women living in USA, ',year,sep=''))
png(paste('map_',year,'.png',sep=''),width=1500,height=1000)
print(map2)
dev.off()
}
#Combine all the maps into one pdf
map3 <- map1 +
scale_fill_brewer(palette='Blues')+
geom_polygon(data=dsmap,aes(x=long,y=lat,group=group,fill=Population),color='black')+
facet_wrap(~YEAR)
ggsave('map304.pdf', width = 10, height = 7.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment