使用tmap在形状上弹出 [英] Popup on a shape using tmap
问题描述
我已经制作了一张地图,取消了tmap
的引用,以使其包含在使用leaflet
的闪亮应用程序中.我大致想要的是:具有基于SpatialPolygonsDataFrame的填充颜色的专题图,当您单击该图时,将弹出一个带有多边形信息的弹出窗口.我想在单击时更改弹出窗口以获得更好的布局.默认情况下,将显示数据集中的名称,但实际上并不是用户友好的名称.
这是一个可重现的示例.
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)
我试图命名向量(popup.vars = c("Life Expectancy" = "life_exp", "Well being" = "well_being)
,但这不起作用.
我还尝试在对leaflet::addPolygons
的调用中添加弹出窗口,但收到错误消息.
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))
derivePolygons(数据,lng,lat,missing(lng),missing(lat),"addPolygons")中的错误: 找不到多边形数据;请为addPolygons提供数据和/或lng/lat参数
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
谢谢
推荐答案
在开发版本中,popup.vars的向量名称现在用作标签.另外,我还向每个图层函数添加了popup.format.您可以分别为每个变量指定数字格式.
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)))
这篇关于使用tmap在形状上弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!