使用tmap在形状上弹出

Flo*_*nGD 1 r popup leaflet tmap

我已经制作了一张地图,无法tmap在一个闪亮的应用程序中使用leaflet.我大致有我想要的东西:一个基于SpatialPolygonsDataFrame的填充颜色的专题地图,当你点击地图时,弹出一个关于多边形的额外信息.我想在点击时更改弹出窗口以获得更好的布局.默认情况下,会显示数据集中的名称,但它实际上并不是用户友好的.
这是一个可重复的例子.

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)
Run Code Online (Sandbox Code Playgroud)

我试图命名向量(popup.vars = c("Life Expectancy" = "life_exp", "Well being" = "well_being),但这不起作用.
我也尝试在调用时添加弹出窗口leaflet::addPolygons,但是我收到一条错误消息.

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))
Run Code Online (Sandbox Code Playgroud)

derivePolygons(data,lng,lat,missing(lng),missing(lat),"addPolygons")出错:找不到多边形数据; 请为addPolygons提供数据和/或lng/lat参数

谢谢

Mar*_*kes 7

在开发版本中,popup.vars的矢量名称现在用作标签.另外,我已经为每个图层功能添加了popup.format.您可以分别指定每个变量的数字格式.

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)))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


tim*_*lio 5

免责声明:黑客

我首先警告这是一个黑客攻击,但代码应该可以实现您的目标。也许,可以在存储库上提出问题tmap以获取其他弹出选项。

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
Run Code Online (Sandbox Code Playgroud)

带有格式化弹出窗口的地图屏幕截图