Search code examples
rpngmapdeck

PNG of Static Map using R and Mapdeck


I would like to create the static map using R and Mapdeck. I am doing it for the first time, and I just want just any png map output at this stage. (Eventually I want to create USA map with 50 states and with extended horizontal bar charts for some cities.)

On the website below I found a sample code. At the moment I just want a simple png of this file. I created mapdeck token, I can run this code with no errors, but I do not get any "viewer" output that would display the map that I would export to png. I have found other mapdeck/R codes on the web, and none of them display any visuals.

https://geocompr.robinlovelace.net/adv-map.html

library(mapdeck)
set_token(Sys.getenv("MAPBOX"))
df = read.csv("https://git.io/geocompr-mapdeck")
ms = mapdeck_style("dark")
mapdeck(style = ms, pitch = 45, location = c(0, 52), zoom     = 4) %>%
add_grid(data = df, lat = "lat", lon = "lng", cell_size = 1000,
     elevation_scale = 50, layer_id = "grid_layer",
     colour_range = viridisLite::plasma(5))

Solution

  • There are a couple of possible issues

    1. A known issue where you can't see the map in your RStudio viewer

    The solution is to open it in a browser by pressing the 'open in new window' button

    1. There are NAs in the data, and it's focusing you on (0,0)

    The solution to this is to either remove the NAs, or simply zoom out

    Also, the package has been updated to v0.2.1 since that article was written, so there's a couple of subtle changes to the code.

    df = read.csv("https://git.io/geocompr-mapdeck")
    
    library(mapdeck)
    
    set_token("MAPBOX_TOKEN")
    ms = mapdeck_style("dark")
    
    df <- df[ !is.na(df$lat), ]
    
    mapdeck(style = ms, pitch = 45) %>%
      add_grid(data = df, lat = "lat", lon = "lng", cell_size = 1000,
               elevation_scale = 50, layer_id = "grid_layer",
               colour_range = colourvalues::colour_values(1:6, palette = "plasma"))
    

    Edit: corrected code.

    enter image description here