A micro-intro to ggmap

Easy mapping in R

This describes what we did in the break-out session I facilitated for the illustrious Max Richman's Open Mapping workshop at Open Data Day DC. For more detail, I recommend the original paper on ggmap.

ggmap is an R package that does two main things to make our lives easier:

To install ggmap in R:

install.packages("ggmap")

Then you can load the package.

library(ggmap)
## Loading required package: ggplot2

One thing that ggmap offers is easy geocoding with the geocode function. Here we get the latitude and longitude of The World Bank:

address <- "1818 H St NW, Washington, DC 20433"
(addressll <- geocode(address))
##      lon  lat
## 1 -77.04 38.9

The ggmap package makes it easy to get quick maps with the qmap function. There are a number of options available from various sources:

# A raster map from Google
qmap("Washington, DC", zoom = 13)

plot of chunk unnamed-chunk-4


# An artistic map from Stamen
qmap("Washington, DC", zoom = 13, source = "stamen", maptype = "watercolor")

plot of chunk unnamed-chunk-4

Since we were at The World Bank, here's a quick map showing where we were. This shows for the first time how ggplot2 functions (geom_point here) work with ggmap.

bankmap <- qmap(address, zoom = 16, source = "stamen", maptype = "toner")
bankmap + geom_point(data = addressll, aes(x = lon, y = lat), color = "red", 
    size = 10)

plot of chunk unnamed-chunk-5

To connect with Max's demo, we can load in his data about cities in Ghana.

(ghana_cities <- read.csv("ghana_city_pop.csv"))
##    cityID         CityName Census1970 Census1984 Census2000 Estimates2007
## 1       1            Accra     564194     867459    1659136       2096653
## 2       2           Kumasi     260286     489586    1171311       1604909
## 3       3           Tamale      83653     135952     293879        390730
## 4       4 Sekondi-Takoradi      58161      61484     175438        260651
## 5       5         Ashiaman      22549      50918     150312        228509
## 6       6          Sunyani      28780      40634     100992        210748
## 7       7       Cape Coast      51653      57224     118105        154204
## 8       8           Obuasi      31005      60617     115564        147613
## 9       9           Teshie      39382      59552      92359        154513
## 10     10             Tema      60767     100052     141479        161106
##    Estimates2013        Region latitude longitude
## 1        2291352 Greater Accra    5.550  -0.20000
## 2        2069350       Ashanti    6.667  -1.61667
## 3         562919      Northern    9.408  -0.85333
## 4         445205       Western    4.917  -1.76667
## 5         298841 Greater Accra    5.700  -0.03333
## 6         248496   Brong-Ahafo    7.333  -2.33333
## 7         227269       Central    5.100  -1.25000
## 8         180334       Ashanti    6.200  -1.68333
## 9         176597 Greater Accra    5.583  -0.10000
## 10        161612 Greater Accra    5.667   0.00000

We'll pull in a Google map of Ghana and then put dots for the cities, sized based on estimated 2013 population.

ghanamap <- qmap("Ghana", zoom = 7)
ghanamap + geom_point(data = ghana_cities, aes(x = longitude, y = latitude, 
    size = Estimates2013), color = "red") + theme(legend.position = "none")

plot of chunk unnamed-chunk-7

Another useful feature to note is the gglocator function, which let's you click on a map and get the latitude and longitude of where you clicked.

gglocator()

This is all the tip of the iceberg. You'll probably want to know more about ggplot2 if you're going to make extensive use of ggmap. RMaps is another (and totally different) great way to do maps in R.