How to Make Hyperlinks in The Pop-Up of a Tm_Bubbles Item

Is there a way to make hyperlinks in the pop-up of a tm_bubbles item?

Prior to release 3.3-1 of {tmap} (March 2021) you had less control over popups in tmap. The world has changed since, and you may consider this answer deprecated - using {leaflet} directly still works, but it is no longer the only way to make hyperlinks work in popups.

For an example of a purely {tmap} workflow please see my newer answer.

It is much easier to utilize leaflet library directly, via package leaflet.

I first paste together the text of the popup as HTML and then use it in leaflet::addCircleMarkers() calls as popup argument (the tilde is important).

The circle markers from leaflet are very much like tmap bubbles, and can be tuned to look much finer than this; for the sake of brevity of code I have concentrated on the main topic, i.e. customizable hyperlinks in popup balloons of an interactive map.

library(dplyr)   # for mutate & pipe
library(tmap) # for the metro dataset
library(leaflet) # interface to leaflet

data("metro") # from the tmap package

metro %>%
mutate(label = paste("marvel at ", name, " and follow <a href = https://stackoverflow.com/>stack overflow</a>")) %>%
leaflet() %>%
addTiles(group = "OSM") %>%
addCircleMarkers(popup = ~label)

screenshot

Popup on a shape using tmap

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)))

Sample Image

R Shiny Leaflet/Mapview map average value on a Choropleth map

This is because your example data is all on the exact same location, there is no reason the mapping function would be expected to average overlapping points. As well if you did you would not have the data from all the different years. I think what you are looking for is clustering, which I don't think is possible in mapview, you can do it with leaflet or tmap though, ex:

output$mapview =  renderLeaflet({
Map_data <- Temp_map()
TMAP_MAP <- tm_basemap()+
tm_shape(Map_data)+
tm_dots(clustering=TRUE, col=input$Temp, popup.vars=TRUE)
tmap_leaflet(TMAP_MAP)
})



Related Topics



Leave a reply



Submit