0

I'm creating an interactive map of the Basque Country (in Spain) using tmap, leaflet and shiny. I've been able to create the map, but I cannot edit the default behavior of renderLeaflet, specifically:

  1. I'd like to edit the default labels and popups. Ideally I'd like no popups (on click), only labels (on mouseover); the label would contain info currently shown in the popup, plus some additional info, and appear on the top right corner of the map instead of on the area (something like this https://leafletjs.com/examples/choropleth/)

  2. I'd like to remove the leaflet tiles so that only Esri.WorldGrayCanvas is shown, and therefore, there should be no option to switch between tiles.

The following code shows what I'm talking about. The required shape file can be downloaded here ftp://ftp.geo.euskadi.net/cartografia/Limites/ or here https://www.dropbox.com/s/zx3bg5131pomyfy/CB_COMARCAS_5000_ETRS89.shp?dl=0

    library(shiny)
library(shinyWidgets)
library(leaflet)
library(rgdal)
library(tmap)
library(tmaptools)
library(dplyr)


setwd(~)

shape <- read_shape(file="CB_COMARCAS_5000_ETRS89.shp") # Load shape file

shape$COMARCA <- as.character(shape$COMARCA)

shape$COMARCA <- c(# Vector with names of the areas
  "Alto Deba", "Arratia-Nervión", "Bajo Bidasoa", "Bajo Deba", "Cantábrica Alavesa",
  "Donostialdea", "Duranguesado", "Encartaciones", "Estribaciones del Gorbea",
  "Gernika-Bermeo", "Goierri", "Gran Bilbao", "Llanada Alavesa", "Markina-Ondarroa", 
  "Montaña Alavesa", "Plentzia-Mungia", "Rioja Alavesa", "Tolosa", "Urola Costa", "Valles Alaveses")

datawvals$COMARCA <- c(# Vector with names of the areas
  "Alto Deba", "Arratia-Nervión", "Bajo Bidasoa", "Bajo Deba", "Cantábrica Alavesa",
  "Donostialdea", "Duranguesado", "Encartaciones", "Estribaciones del Gorbea",
  "Gernika-Bermeo", "Goierri", "Gran Bilbao", "Llanada Alavesa", "Markina-Ondarroa", 
  "Montaña Alavesa", "Plentzia-Mungia", "Rioja Alavesa", "Tolosa", "Urola Costa", "Valles Alaveses")

datawvals <- bind_rows(datawvals, datawvals, datawvals)
datawvals$time <- c(rep("2010",20), rep("2014",20), rep("2016",20))
datawvals$var1 <- runif(60)
datawvals$var2 <- runif(60)
datawvals$var3 <- runif(60)

data4map <- left_join(shape, datawvals, by="COMARCA") #join shape with data to be depicted

years <- c(2010,2014,2016)


runApp(list(
  ui = fluidPage(
    titlePanel("Shiny tmap!"),
    sidebarLayout(
      sidebarPanel(
        selectInput("var", label = "Variable", choices = c("var1", "var2", "var3"), selected = "var1"),
        sliderTextInput(
          inputId = "time", label = "year", 
          choices = years,  selected = 2010, 
          grid = FALSE
        )
      ),
      mainPanel(
        leafletOutput("map", width=600)
      )
    )
  ),
  server = function(input, output) {

    #Subset data based on year
    subsetData <- reactive({
      new_data <- data4map[data4map$time==input$time, ]
      return(new_data)
    })

    #Different color palettes for diff vars
    colores <- reactive({
      if(input$var=="var1"){
        colores0 <- "Blues"
      } else{
        colores0 <- "Reds"
      }

      return(colores0)
    })

    #Actual plot
    output$map = renderLeaflet({

        tm <- tm_shape(subsetData()) +
          tm_polygons(input$var, palette=colores())


      tmap_leaflet(tm)
    })
  }
))

Thanks in advance for any tips and suggestions on how to edit these defaults.

Ainhoa
  • 191
  • 7

1 Answers1

1

Do you need to use tmap? I think you can get the behaviour you want just with leaflet - have a look at the guide here: https://rstudio.github.io/leaflet/choropleths.html

A workable structure would be:

output$map <- renderLeaflet({

  leaflet() %>%
    addProviderTiles("ESRI.WorldGrayCanvas") %>%
    addPolygons(data = subset_data(), ...) %>%
    addLegend(position = "topright", ...)    # Using ... here to mean more stuff needed!

})

You can then change the colours of the shapes by passing color and fill arguments to addPolygons; you can change the popups and labels with the popup and label arguments respectively.

In the legend (and for the polygon colours), you'll need to supply a colour palette and values

All the details are on the link above -it's the same choropleth as in the leafletjs link you provided, but built using R instead.

cwthom
  • 393
  • 1
  • 13
  • 1
    Thanks, I'll give it a try. I started using tmap because it seemed easier, but I'll look into using only leaflet as you suggest and let you know. – Ainhoa Jun 13 '19 at 05:00