Wednesday, May 27, 2015

Creating Styled Google Maps in ggmap

In R, my go to package for creating maps is ggmap. Using this, one can create maps using a variety of services, including Google maps. I just recently discovered the ability to create a styled version of a Google map. Let's go through this process to create a black and white map of Chicago, with the parks shown in red.

First, go to the Styled Maps Wizard and type in “Chicago IL”.



You are shown the stock Google map of Chicago, with some sliders to change various attributes of the map.


Let's take the “saturation” slider down to -100 and bump up the “gamma” slider to .5.


Then, we can work on making the parks red. First, click the “Add” button on the right side.


Next, on the left side at the top, choose “Point of Interest”, then “Park”.


Then, click the “Color” box and type in “#ff0000” into the box to specify red for the parks You should see the parks highlighted in red now.


The last thing we need is to grab the JSON for these styles. On the right side, at the bottom of the “Map Style” box, click “Show JSON”.


With this JSON, we can create the map in ggmaps.

library("RJSONIO")
library("ggmap")
library("magrittr")
style <- '[
  {
    "stylers": [
      { "saturation": -100 },
      { "gamma": 0.5 }
    ]
  },{
    "featureType": "poi.park",
    "stylers": [
      { "color": "#ff0000" }
    ]
  }
]'
style_list <- fromJSON(style, asText=TRUE)

create_style_string<- function(style_list){
  style_string <- ""
  for(i in 1:length(style_list)){
    if("featureType" %in% names(style_list[[i]])){
      style_string <- paste0(style_string, "feature:", 
                             style_list[[i]]$featureType, "|")      
    }
    elements <- style_list[[i]]$stylers
    a <- lapply(elements, function(x)paste0(names(x), ":", x)) %>%
           unlist() %>%
           paste0(collapse="|")
    style_string <- paste0(style_string, a)
    if(i < length(style_list)){
      style_string <- paste0(style_string, "&style=")       
    }
  }  
  # google wants 0xff0000 not #ff0000
  style_string <- gsub("#", "0x", style_string)
  return(style_string)
}

style_string <- create_style_string(style_list)
mymap <- ggmap(get_googlemap("chicago", size=c(800,800), style=style_string), extent="device")
ggsave(filename="pics/mymap.png", width=8, height=8)



I wrote the helper function “create_style_string” to take the style parameters given from Google as JSON, and create a string to use in the API request. Put that style_string in the “style” parameter in get_googlemap() and you get your map back.

The possibilities are endless to create maps with colored placenames, water, places of interest, etc. Very cool stuff.

3 comments:

  1. The maps and codes you show us are very powerful and different from what I learned in school. Also, your example is closer to the real life. In the school, I did not learn in such deep about the package ggmap, and I found the package ggmap is even more powerful than what I thought after reading this. I play a little with your three packages, and to me, the way you state those commands are really clear and effective. Your interpretation of the coding is what exactly I am looking for, and I can get access to them whenever I want. Thanks for explaining such a good example to us.

    ReplyDelete
    Replies
    1. I'm glad it helped. Good luck with your mapping endeavors in R!

      Delete

Note: Only a member of this blog may post a comment.