World map with ggmap

后端 未结 4 1860
無奈伤痛
無奈伤痛 2020-11-29 03:43

I am using ggmap and wish to have a map of the world centered on Australia to which I can easily plot geocoded points. ggmap seems to be a lot easier to use compared to some

4条回答
  •  清酒与你
    2020-11-29 04:20

    I recently got the same error and it boiled down to ggmap not liking latitudes outside $\pm$ 80°.

    However, I had to download my image separately as it was too large for a download (with OSM); this is not your problem, but I record it for future readers.

    Here's how I solved it:

    • separate download of a Mercator projected image via BigMap
    • The latitude needed some care: I got the same errors you show with latitude limits outside $\pm$ 80° when I expected everything should be fine till the 85° OSM covers), but I didn't track them down since I anyways don't need the very high latitudes.
    • 0°/0° center was good for my purpose (I'm in Europe :-)), but you can certainly cut the image whereever it is good for you and wrap it yourself by cbind. Just make sure you know the longitude of your cut.
    • then set the bounding box of your image
    • and assign the appropriate classes

    Here's what I do:

    require ("ggmap")
    library ("png")
    
    zoom <- 2
    map <- readPNG (sprintf ("mapquest-world-%i.png", zoom))
    map <- as.raster(apply(map, 2, rgb))
    
    # cut map to what I really need
    pxymin <- LonLat2XY (-180,73,zoom+8)$Y # zoom + 8 gives pixels in the big map
    pxymax <- LonLat2XY (180,-60,zoom+8)$Y # this may or may not work with google
                                           # zoom values
    map <- map [pxymin : pxymax,]
    
    # set bounding box
    attr(map, "bb") <- data.frame (ll.lat = XY2LonLat (0, pxymax + 1, zoom+8)$lat, 
                                      ll.lon = -180, 
                                      ur.lat = round (XY2LonLat (0, pxymin, zoom+8)$lat), 
                                      ur.lon = 180)
    class(map) <- c("ggmap", "raster")
    
    ggmap (map) + 
      geom_point (data = data.frame (lat = runif (10, min = -60 , max = 73), 
                                     lon = runif (10, min = -180, max = 180)))
    

    result:
    ggplot world map

    Edit: I played a bit around with your google map, but I didn't get the latitudes correct. :-(

提交回复
热议问题