Mapmaking with ggmap
I am always looking for free alternatives to ArcGIS for making pretty maps. R is great for graphics and the new-to-me ggmap package is no exception.
I’m working with some data from Botswana for a contract and needed to plot maps for several years of count based data, where the GPS coordinates for facilities were known. ArcGIS is unwieldy for creating multiple maps of the same type of data based on time points, so R is an ideal choice…. the trouble is the maps I can easily make don’t look all that good (though with tweaking can be made to look better.)
ggmap offered me an easy solution. It downloads a topographic base map from Google and I can easily overlay proportionally sized points represent counts at various geo-located points. This is just a map of Botswanan health facilities (downloaded from Humanitarian Data Exchange) with the square of counts chosen from a normal distribution. The results are rather nice.
library(rgdal)
library(ggmap)
library(scales)
#read in grographic extent and boundary for bots
btw <- admin<-readOGR(“GIS Layers/Admin”,”BWA_adm2″) #from DIVA-GIS
# fortify bots boundary for ggplot
btw_df <- fortify(btw)
# get a basemap
btw_basemap <- get_map(location = “botswana”, zoom = 6)
# get the hf data
HFs.open.street.map<-read.csv(“BotswanaHealthFacilitiesOpenStreetMap.csv”)
# create random counts
HFs.open.street.map$Counts<-round((rnorm(112,mean=10,sd=5))^2,0)
# Plot this dog
plot
ggmap(btw_basemap) +
geom_polygon(data=btw_df, aes(x=long, y=lat, group=group), fill=”red”, alpha=0.1) +
geom_point(data=HFs.open.street.map, aes(x=X, y=Y, size=Counts, fill=Counts), shape=21, alpha=0.8) +
scale_size_continuous(range = c(2, 12), breaks=pretty_breaks(5)) +
scale_fill_distiller(breaks = pretty_breaks(5))