Translate

Wednesday, 25 December 2013

Mapping in R with Google Maps

'RgoogleMaps' package allows R users to download maps from Google Maps and plot locations of interest within R. Usually, the following packages are also required to support and supplement functions provided this package. The additional packages are:

  • 'png'
  • 'RJSONIO'
  • 'bitops'
  • 'RCurl'
  • 'ggplot2'
  • 'ggmap'
These packages allow you to connect to Google and geocode the locations of your interest, meaning obtaining longitude and latitude, and allow you to download a map region that captures these geocodes with an appropriate zoom size.

To download a map, you would first need a reference point in longitude and latitude where the map can be centred on. In the below example, I will be obtaining a map of Australia using Alice Spring, the city in the middle of Australia as a reference point. You can obtain the geocode of the location from Google as shown in the example.

u <- ("http://maps.google.com/maps/api/geocode/json?sensor=false&address=Alice Spring,Australia") u1 <- URLencode(u, reserved = FALSE) doc <- getURL(u1) x <- fromJSON(doc, simplify = FALSE) lat <- x$results[[1]]$geometry$location$lat
lng <- x$results[[1]]$geometry$location$lng


Then, you can download the map as shown below using the above longitude and latitude. You may need to make some manual adjustments to centre the map to the position you like. In this example, I have made adjustments by subtracting 15 from the latitude obtained above. You need to assign the zoom size and a type of map.

Map1 <- get_map(location = c(lng, lat-15), zoom = 4, maptype = "roadmap")


When you run the above line to download a map, you will receive the below warning. This is Google's Terms of Service, and you may need to read this as it may impact on your intended activity.
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=-30,135&zoom=4&size=%20640x640&scale=%202&maptype=roadmap&sensor=false## Google Maps API Terms of Service : http://developers.google.com/maps/terms
To display the downloaded map, ggmap() is used as shown below.

ggmap(Map1)

plot of chunk unnamed-chunk-4

As mentioned earlier, there are other types of maps available. They include 'road', 'satellite', 'terrain', 'hybrid' (satellite map with location names displayed), 'toner', 'watercolor' etc.

The below example is a satellite version of the above.

Map2 <- get_map(location = c(lng,lat-15), zoom = 4, maptype = "satellite")


If you need black & white background, you set the 'color' parameter as shown below.

Map2 <- get_map(location = c(lng,lat-15), zoom = 4, maptype = "roadmap", color="bw")


To identify locations of interest on the map, you need to obtain the geocodes of the locations you want to identify first in a similar way to when you identified a reference location to centre the map. Then, you can plot the locations in a similar manner to using ggplot functions but using ggmap(). The below example will plot Perth, Sydney, Brisbane and Darwin on the map.

GCode <- data.frame() for (A in c("Perth", "Sydney", "Brisbane", "Darwin")) { u <- paste("http://maps.google.com/maps/api/geocode/json?sensor=false&address=", A, ",Australia", sep = "") u1 <- URLencode(u, reserved = FALSE) doc <- getURL(u1) x <- fromJSON(doc, simplify = FALSE) lat <- x$results[[1]]$geometry$location$lat lng <- x$results[[1]]$geometry$location$lng GCodeX <- data.frame(City = A, LAT = lat, LONG = lng) GCode <- rbind(GCode, GCodeX) } Mapx <- get_map(location = c(lng,lat-15), zoom = 4, maptype = "roadmap")

ggmap(Mapx) + geom_point(data = GCode, aes(x = GCode$LONG, y = GCode$LAT), size = 5, alpha = 0.6, col = "red")


plot of chunk unnamed-chunk-1