将鼠标悬停在传单中的国家/地区上以查看其名称
Mousehover over country in leaflet to see its name
我正在创建世界地图:
library(leaflet)
library(rnaturalearth)
countries <- rnaturalearth::countries110
mymap <- leaflet(countries)
mymap %>% addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1)
是否可以将鼠标悬停在任何国家/地区上以查看其名称或单击任何国家/地区以查看其名称?
非常感谢!
我找到了解决办法。
首先,使用库 "htmltools" 创建标签列表
然后 lapply 它在标签 = 下。所以,最终的代码是:
library(leaflet)
library(rnaturalearth)
library(htmltools)
countries <- rnaturalearth::countries110
mymap <- leaflet(countries)
labs <- as.list(countries$name)
mymap %>% addPolygons(stroke = FALSE, smoothFactor = 0.2,
fillOpacity = 1, label = lapply(labs, HTML))
或者,更简单:
library(leaflet)
library(rnaturalearth)
countries <- rnaturalearth::countries110
mymap <- leaflet(countries)
mymap %>% addPolygons(stroke = FALSE, smoothFactor = 0.2,
fillOpacity = 1, label = ~name)
只要标签数据是要绘制的数据框的一部分,~
符号就很有魅力。
我正在创建世界地图:
library(leaflet)
library(rnaturalearth)
countries <- rnaturalearth::countries110
mymap <- leaflet(countries)
mymap %>% addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1)
是否可以将鼠标悬停在任何国家/地区上以查看其名称或单击任何国家/地区以查看其名称?
非常感谢!
我找到了解决办法。 首先,使用库 "htmltools" 创建标签列表 然后 lapply 它在标签 = 下。所以,最终的代码是:
library(leaflet)
library(rnaturalearth)
library(htmltools)
countries <- rnaturalearth::countries110
mymap <- leaflet(countries)
labs <- as.list(countries$name)
mymap %>% addPolygons(stroke = FALSE, smoothFactor = 0.2,
fillOpacity = 1, label = lapply(labs, HTML))
或者,更简单:
library(leaflet)
library(rnaturalearth)
countries <- rnaturalearth::countries110
mymap <- leaflet(countries)
mymap %>% addPolygons(stroke = FALSE, smoothFactor = 0.2,
fillOpacity = 1, label = ~name)
只要标签数据是要绘制的数据框的一部分,~
符号就很有魅力。