Search code examples
rggmapterrain

How do I change the maptype in ggmap?


I am having trouble changing the background of my ggmap. When I try to change the maptype it always comes out as terrain. I would like to change it to satellite. Any suggestions? Here is my code:

 library(ggmap)
 # Long and Lat Coordinates
 Cuba_all <- data.frame("Longitude" = c(-79.79623, -79.42313, -79.01722, -80.29218, -80.50040, -80.51981, -80.36674, -79.87957, -79.66906, -79.76122, -80.26587, -79.91689, -80.10454, -80.22530, -80.12910, -79.98889, -79.84307, -79.81694, -80.22201, -80.48088, -80.44482, -80.29068, -80.36213, -80.50879, -80.29634), "Latitude" = c(22.06622, 22.14845, 22.20900, 22.05258, 22.30107, 22.88154, 22.70679, 22.53541, 22.39237, 22.35697, 21.91868, 22.08949, 21.83760, 22.10561, 22.11061, 22.02766, 22.04936, 22.37516, 22.56684, 22.44313, 22.44416, 22.50470, 22.75872, 22.35473, 22.49178))
 # Create Cuba Dimensions
 sbbox <- make_bbox(lon = Cuba_all$Longitude, lat = Cuba_all$Latitude, f = .1)
 sbbox
 # Grab Map of Cuba
 sq_map <- get_map(location = sbbox, source = "google", maptype = "satellite")
 # Plot Map
 ggmap(sq_map)

Solution

  • @42's comment is partly right (as far as I can tell). But it's not necessarily your API key that's the problem, it's your location specification. The Google map server wants location specified as lon/lat at the center, plus a zoom factor. get_map() silently decides to get a Stamen terrain map if you submit the location in bounding box format; this appears to me to be a bug (or "infelicity") in get_map(), and is the subject of at least two issues on the ggmap issues list.

    In any case, when I specified the right lon/lat vector, and played around with the zoom factor until it's about right (I don't know how to do this other than trial and error ...), it worked for me.

    sm <- with(Cuba_all,c(lon=median(Longitude),lat=median(Latitude)))
    sq_map1 <- get_map(location = sm, zoom=9,
                       source = "google", maptype = "satellite")
    ggmap(sq_map1)+
        geom_point(data=Cuba_all,aes(x=Longitude,y=Latitude),colour="red")
    ggsave("cuba.png")
    

    enter image description here


    A little more digging in get_map() makes it look like this may have been an oversight by the developers. It seems to have been fixed in the development version of ggmap, but hasn't made it to the CRAN version yet (see comments here.)

    This code chunk:

    if (is.numeric(location) && length(location) == 4) {
        location_type <- "bbox"
        location_stop <- FALSE
        source <- "stamen"
        maptype <- "terrain"
        ## ...
    

    detects that the location has been given as a bounding box and automatically sets source to "stamen" and maptype to "terrain". Later on there's code that looks like it should give a warning if you pass a bounding box with source=="google", but it can't ever be reached (because source will have been changed to "stamen" by this point) ...

    if (source == "google") {
        if (location_type == "bbox") {
            warning("bounding box given to google - spatial extent only approximate.", 
                call. = FALSE, immediate. = TRUE)
            message("converting bounding box to center/zoom specification. (experimental)")
            user_bbox <- location
            location <- c(lon = mean(location[c("left", "right")]), 
                lat = mean(location[c("bottom", "top")]))
        }