Skip to content

Instantly share code, notes, and snippets.

@SirSamAlot280
Created October 15, 2016 16:12
Show Gist options
  • Save SirSamAlot280/64d9d3dee40db05d1ae35a69120bd104 to your computer and use it in GitHub Desktop.
Save SirSamAlot280/64d9d3dee40db05d1ae35a69120bd104 to your computer and use it in GitHub Desktop.
Lab_5_Mapping_Black_Population
#Load packages
library(readr)
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(ggmap)
library(maptools)
library(gtools)
library('gganimate')
#Read in maps
mapdata <- read_csv('data/map.csv')
#Create a map of the United States
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()
#Read in selected IPUMS data
ipums <- read_csv('./data/10_13.csv',col_types=cols(PERWT=col_double()))
#Filter data to include those born in the South according to IPUMS between 1885 and 1900
dsouth <- ipums %>% filter(BPL %in% c(1,5,10,11,12,13,21,22,24,28,37,40,45,47,48,51,54) & YEAR-AGE>=1885 & YEAR-AGE<=1900)
#Include only individuals who have raically been identified as Black or partially black (mulatto,quadroon,etc.)
dsraceblack <- dsouth %>% filter(RACESINGD %in% c(20,21))
#Group by the State an inividual lives in and the given year
ds <- dsraceblack %>% group_by(YEAR,STATEFIP) %>% summarise(Number=sum(PERWT))
#Recode the STATEFIP varaible in the U.S. map to match with the dataset
newmap <- mapdata %>% mutate(STATEI=as.numeric(STATEFIP))
#Join the dataset to the U.S. map
dsmap <- left_join(ds,newmap,by=c('STATEFIP'='STATEI'))
#Order map data by year
dsmap <- dsmap %>% arrange(order)
#Create new map with the joined data
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()
#Determine best range for categorization
cuts <- quantcut(ds$Number,q=seq(0,1,.2))
table(cuts)
#Create new categories
dscats <- ds %>% mutate(Population=factor(ifelse(Number<601,1,
ifelse(Number<3130,2,
ifelse(Number<19900,3,
ifelse(Number<64300,4,5))))))
levels(dscats$Population) <- c('1-600','601-3,129','3,130-19,899','19,900-64,299','64,300+')
#Reattach mapdata and arrange the order for the years
dsmap <- left_join(dscats,newmap,by=c('STATEFIP'='STATEI')) %>% arrange(order)
#Create a map to create one color with varying hues
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 forloop to create a map for every year
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('Blacks Born 1885-1900 in the South,',year,sep=' '))
png(paste('map_',year,'.png',sep=''),width=1500,height=1000)
print(map2)
dev.off()}
#Combine every map year produce and animate
map2 <- map1 + scale_fill_brewer(palette = 'Blues') +
geom_polygon(data=dsmap,aes(x=long,y=lat,group=group,fill=Population,frame=YEAR),color='black') +
labs(title='Blacks Born Between 1885-1900 in the South,')
gg_animate(map2,ani.width=1500,ani.height=1000,'anmap.gif')
png('map.png',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