Creating Styled Google Maps in ggmap

[This article was first published on Stats and things, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

To leave a comment for the author, please follow the link and comment on their blog: Stats and things.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)