Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

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

I'm using the tmap package to visualise some values in a map. Does anyone know a way to integrate hyperlinks in the popup box of an interactive map?

What I did was use the code below. I do see the desired URL, but one can't click it.

Then I added < A HREF="URL">name< / A > to the string. But that doesn't make it work.

tmap_mode("view")

tm_shape(dataset) +
  tm_bubbles(size = 1, col = "value1", palette = "-RdBu", popup.vars = c("value2","URL"), text = "value3")

I would appreciate any help in discovering if this is possible at all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...