I have made a map unsing tmap
to include in a shiny app using leaflet
. I have roughly what I want: a thematic map with fill color based on a SpatialPolygonsDataFrame, and when you click the map, a popup with extra information on the polygon. I would like to change the popup for a better layout when clicking. By default, the name in the dataset is displayed, but it is not really user friendly.
Here is a reproducible example.
library(tmap)
library(leaflet)
data(Europe)
tmap_mode("view")
carte <- tm_shape(Europe) +
tm_borders(alpha = 0.5) +
tm_fill(col = "well_being",
id = "name",
popup.vars = c("life_exp","well_being"))
tmap_leaflet(carte)
I have tried to name the vector (popup.vars = c("Life Expectancy" = "life_exp", "Well being" = "well_being)
, but this doesn't work.
I have also tried to add the popup on a call to leaflet::addPolygons
, but I get an error message.
carte2 <- tm_shape(Europe) +
tm_borders(alpha = 0.5) +
tm_fill(col = "well_being")
nom <- Europe$name
tmap_leaflet(carte2) %>%
addPolygons(layerId = nom,
popup = paste0("<b>",~name,"</b><br/>Life Expectancy : ",
~life_exp," <br/>Well being : ", ~well_being))
Error in derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolygons") : Polygon data not found; please provide addPolygons with data and/or lng/lat arguments
Thanks