问题
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
回答1:
In the development version, vector names of popup.vars are now used as labels. Also, I've added popup.format to each layer function. You can specify the number formatting for each variable separately.
data(World, metro)
metro$growth <- (metro$pop2020 - metro$pop2010) / (metro$pop2010 * 10) * 100
ttm()
tm_shape(metro) +
tm_bubbles("pop2010", col = "growth",
border.col = "black", border.alpha = .5,
style="fixed", breaks=c(-Inf, seq(0, 6, by=2), Inf),
palette="-RdYlBu", contrast=1,
title.size="Metro population",
title.col="Growth rate (%)", id="name",
popup.vars=c("Population (2010)"="pop2010", "Population (2020)"="pop2020", "Growth (%)"="growth"),
popup.format=list(growth=list(digits=4)))
回答2:
Disclaimer: Hack
I will start by warning that this is a hack, but the code should accomplish your objective. Perhaps, file an issue on the tmap
repo for additional popup options.
library(tmap)
data(Europe)
carte2 <- tm_shape(Europe) +
tm_borders(alpha = 0.5) +
tm_fill(col = "well_being")
# this is a hack, since I do not see a clean mechanism to accomplish
# look at the leaflet map calls for addPolygons
leafmap <- tmap_leaflet(carte2)
# if you are ok using another package
# install.packages("listviewer")
# listviewer::jsonedit(leafmap$x$calls)
# if not then
str(leafmap$x$calls, max.level=2)
# addPolygons is the call we need to adjust
# in this example it is the fourth call
str(leafmap$x$calls[[4]], max.level=2)
# the popups are the fifth element of the args
leafmap$x$calls[[4]]$args[[5]]
# adjust these how you like
leafmap$x$calls[[4]]$args[[5]] <- leaflet:::evalFormula(
~paste0(
"<b>",name,"</b><br/>",
"Life Expectancy : ", life_exp,
" <br/>Well being : ", format(well_being, digits=4)
),
data=Europe
)
# warned this is a hack
来源:https://stackoverflow.com/questions/41940403/popup-on-a-shape-using-tmap