问题
I have customized a style for use with ggmap
using https://mapstyle.withgoogle.com/. My question is how to integrate the JSON into my R code.
I tried the following code to no avail:
map <- get_googlemap(center = 'London', zoom = 15,
style = c('element:labels|visibility:off', 'feature:road.local|visibility:off'))
ggmap(map)
It works with either of the style commands alone but not together. Where is the bug in my code?
回答1:
I have developed a package ggmapstyles
, which should help with this problem: https://github.com/dr-harper/ggmapstyles
The package lets you select designs from Snazzy Maps, and using the style from the page is as simple as copying the URL into the style:
devtools::install_github("mikey-harper/ggmapstyles")
library(ggmapstyles)
map <- get_snazzymap(center = 'London',
mapRef = "https://snazzymaps.com/style/61/blue-essence")
ggmap(map)
If you don't find a design you like, you can join Snazzy Maps for free and make your own custom design within the web browser.
回答2:
I'm unclear myself on how exactly ggmap expects to receive styling, but get_googlemap
has a parameter to inject a string into the URL sent to the Google Maps API. Based on the Google Maps docs, your strings seem formatted correctly for injecting. You can collapse each of those style specifications into a single string, and give that to the inject
parameter rather than the style
one.
So
stylestr <- sprintf("&style=%s", c("element:labels|visibility:off", "feature:road.local|visibility:off") %>% paste(collapse = "")
will yield the string &style=element:labels|visibility:off&style=feature:road.local|visibility:off
that can be used as your inject
parameter. (I used sprintf
and paste
to make it easy for adding a whole slew of style specs.)
来源:https://stackoverflow.com/questions/43480986/custom-map-style-with-ggmap