Skip to content

Instantly share code, notes, and snippets.

@lfortin-117
Created October 17, 2016 19:50
Show Gist options
  • Save lfortin-117/04c3ff90c4f6f5fc0e9a5f0e100adacf to your computer and use it in GitHub Desktop.
Save lfortin-117/04c3ff90c4f6f5fc0e9a5f0e100adacf to your computer and use it in GitHub Desktop.
Lab 5 - LF
library(readr)
library(dplyr)
library(ggplot2)
library(readr)
library(RColorBrewer)
library(ggmap)
library(maptools)
library(gtools)
#Loading in the blank map of the United States and plotting
mapdata <- read_csv('map.csv')
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()
#Reading the data and separating out the first generation immigrants
ipums <- read_csv('Lab5.csv', col_types = cols(PERWT=col_double()))
immig <- ipums %>% filter(BPL>150)
#Aggregating
ds <- immig %>% group_by(YEAR, STATEFIP) %>% summarise(NUMBER=sum(PERWT))
#Join this data frame to our map data frame and ordering
newmap <- mapdata %>% mutate(STATEI=as.integer(STATEFIP))
dsmap <- left_join(ds, newmap, by=(c('STATEFIP'='STATEI')))
dsmap <- dsmap %>% arrange(order)
cuts <- quantcut(ds$NUMBER, q=seq(0,1,.2))
#Population by number of first generation immigrants in each state
dscats <- ds %>% mutate(Population=factor(ifelse(NUMBER<50000, 1,
ifelse(NUMBER<100000, 2,
ifelse(NUMBER<500000, 3,
ifelse(NUMBER<1000000, 4, 5))))))
levels(dscats$Population) <- c('1-49,999','50,000-99,999','100,000-499,999','500,000-1,000,000','1,000,000+')
#Final join of dataframe to our map data frame
dsmap <- left_join(dscats, newmap, by=c('STATEFIP'='STATEI')) %>% arrange(order)
#Plotting
map2 <- map1 + scale_fill_brewer(palette='Blues') +
geom_polygon(data=dsmap, aes(x=long, y=lat, group=group, fill=Population), color='black') +
labs(title='First Generation Immigrants in the United States 1900-2000,', sep='') +
facet_wrap(~YEAR)
png('map_facet3.png', width=2000, height=800)
print(map2)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment