Skip to content

Instantly share code, notes, and snippets.

/Maps

Created Nov 14, 2016
Embed
What would you like to do?
FP Maps
#figure maps
#load packages
library(dplyr)
library(readr)
library(ggplot2)
library(RColorBrewer)
library(ggmap)
library(maptools)
library(gtools)
#read map data
mapdata <- read_csv("~/Desktop/map.csv")
#make map 1
map1 <- ggplot() + theme_nothing(legend=TRUE) +
geom_polygon(data=mapdata, aes(x=long,y=lat,group=group),fill='white',color='black')
#read csv
ipums <- read_csv('~/Desktop/usa_00028.csv')
#create integer map data
newmap <- mapdata %>% mutate(STATEI=as.integer(STATEFIP))
#women only and over 22
women_only <- ipums%>% filter(SEX==2 & AGE>=22)
#set SLWT for 1950
#calculate total num of women
numwomen_region <- women_only %>% group_by(YEAR,BPL) %>% summarise(Number=sum(PERWT))
#filter to only women with college degrees
women_educ <- women_only %>% filter (EDUCD>=101 & EDUCD<=116)
#calculate number of women w/ college degrees
numwomen_region_educ <- women_educ %>% group_by(YEAR,BPL) %>% summarise(Number=sum(PERWT))
#join the two
joined <- left_join(numwomen_region,numwomen_region_educ,by=c('YEAR'='YEAR',"BPL"="BPL"))
#calculate percent
percentages <- joined %>% mutate(pct=Number.y/Number.x*100)
#rename BPL statefip for map
percentages <- percentages %>% rename(STATEFIP=BPL)
#create integer
newmap <- mapdata %>% mutate(STATEI=as.integer(STATEFIP))
#create categories for percents
pct_cats <- percentages %>% mutate(Percent=factor(ifelse(pct<0,1,
ifelse(pct<0.5,2,
ifelse(pct<2,3,
ifelse(pct<5,4,
ifelse(pct<10,5,
ifelse(pct<15,6,
ifelse(pct<20,6,
ifelse(pct<25,7,
ifelse(pct<30,8,9)))))))))))
levels(pct_cats$Percent) <- c('Under 0.50%','0.50%-1.99%','2.00%-4.99%','5.00%-9.99%','10%-14.99%','15.00%-19.99%','20.00%-24.99%','25.00%-29.99%','30% or Greater')
#make percent map join
pct_map <- left_join(pct_cats,newmap,by=c('STATEFIP'='STATEI'))
pct_map <- pct_map %>% arrange(order)
#make map 3
#make map 4 unique map and print
unique(pct_map$YEAR)
for (year in unique(pct_map$YEAR)) {
map4 <- map1 + scale_fill_brewer(palette='blues',drop=F) +
theme_bw(base_size = 24) +
geom_polygon(data=filter(pct_map,YEAR==year),aes(x=long,y=lat,group=group,fill=Percent),color='black') +
labs(title=paste('Percent of Women Holding College Degrees by Birth State,',year,sep=' '))
png(paste('~/Desktop/map_',year,'.png',sep=''),width=1500,height=1000)
print(map4)
dev.off()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.