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")<br />library("ggmap")<br />library("magrittr")<br />style <- '[<br />  {<br />    "stylers": [<br />      { "saturation": -100 },<br />      { "gamma": 0.5 }<br />    ]<br />  },{<br />    "featureType": "poi.park",<br />    "stylers": [<br />      { "color": "#ff0000" }<br />    ]<br />  }<br />]'<br />style_list <- fromJSON(style, asText=TRUE)<br /><br />create_style_string<- function(style_list){<br />  style_string <- ""<br />  for(i in 1:length(style_list)){<br />    if("featureType" %in% names(style_list[[i]])){<br />      style_string <- paste0(style_string, "feature:", <br />                             style_list[[i]]$featureType, "|")      <br />    }<br />    elements <- style_list[[i]]$stylers<br />    a <- lapply(elements, function(x)paste0(names(x), ":", x)) %>%<br />           unlist() %>%<br />           paste0(collapse="|")<br />    style_string <- paste0(style_string, a)<br />    if(i < length(style_list)){<br />      style_string <- paste0(style_string, "&style=")       <br />    }<br />  }  <br />  # google wants 0xff0000 not #ff0000<br />  style_string <- gsub("#", "0x", style_string)<br />  return(style_string)<br />}<br /><br />style_string <- create_style_string(style_list)<br />mymap <- ggmap(get_googlemap("chicago", size=c(800,800), style=style_string), extent="device")<br />ggsave(filename="pics/mymap.png", width=8, height=8)<br />



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)