As PAA 2018 is approaching, I’m looking forward to talk more about spatial prediction and subnational fertility disaggregation using DHS household survey data and our research on fine grid scale mapping of demographic indicators in low income countries at WorldPop (University of Southampton) and Flowminder.
Come meet me at the session “Adolescent Transitions into Parenthood”, on Saturday 28th of April from 11:15am-12:45pm in Plaza Ballroom E (Sheraton Denver Downtown), I will present our paper “The geography of fertility rates in low and middle-income countries: analysis of cross-sectional surveys from 69 countries”.
I’ve recently came across the hafen/geofacet function and was pondering to blog an example. Then, I came across a perfect example, thanks to kanishkamisra for working on the dataset & code and making it available via github here!
2. Download the data files (note they are not ready for use but need some cleaning as there are more areas in the excel files than polygons in the shape file). I copy here the code as I have used it in my script but it’s available at RPubs thanks to David Robinson.
dt <- read.csv("new_county_election_2016.csv", header=T)
us <- readShapePoly("./USA_adm/USA_adm2.shp")
us0 <- readShapePoly("./USA_adm/USA_adm0.shp")
us.m <- us[-c(which(us$NAME_1=="Alaska")),] #get rid of Alaska
us.d <- us.m[-c(67:71),]
5. Prepare the color palette(s)
nclassint <- 5 #number of colors to be used in the palette
cat.T <- classIntervals(dt$Trump[-c(67:71)], nclassint,style = "jenks") #style refers to how the breaks are created
colpal.T <- brewer.pal(nclassint,"Reds")
color.T <- findColours(cat.T,colpal.T) #sequential
bins.T <- cat.T$brks
lb.T <- length(bins.T)
5. Plot the maps with map basic…
# pdf("Where are the trump voters.pdf")
# plot(us.d, col=color.T, border=F)
# plot(us0,add=T, lwd=0.1)
# legend("bottomleft",fill=colpal.T,legend=paste(round(bins[-length(bins.T)],1),":",round(bins.T[-1],1)),cex=1, bg="white")
# dev.off()
% Votes for Clinton% Votes for Trump
… or ggplot2
library(ggplot2)
library(scales)
theme_set(theme_bw())
ggplot(county_data, aes(Population / Area, Trump)) +
geom_point() +
geom_point(data=county_data[which(county_data$State=="Texas"),], aes(x=Population/Area, y=Trump), colour="red")+
scale_x_log10() +
scale_y_continuous(labels = percent_format()) +
xlab("Population density (ppl / square mile)") +
ylab("% of votes going to Trump") +
geom_text(aes(label = County), vjust = 1, hjust = 1, check_overlap = TRUE) +
geom_smooth(method = "lm") +
ggtitle("Population density vs Trump voters by county (Texas Counties in red)")
This is the code to plot in red points according to State (in red) and to add red labels to those points. The check_overlap=T avoids overlapping labels.
# ggplot(county_data, aes(Population / Area, Trump)) +
# geom_point() +
# geom_point(data=county_data[which(county_data$State=="California"),], aes(x=Population/Area, y=Trump), colour="red")+
# scale_x_log10() +
# scale_y_continuous(labels = percent_format()) +
# xlab("Population density (ppl / square mile)") +
# ylab("% of votes going to Trump") +
# geom_text(data=county_data[which(county_data$State=="California"),], aes(label = ifelse(Trump&gt;.5, as.character(dt$County), "" )), color= "red",size=5,vjust = 1, hjust = 1, check_overlap = TRUE) +
# geom_smooth(method = "lm") +
# ggtitle("Population density vs Trump voters by county (California in red)")