使用 tmap 在形状上弹出 [英] Popup on a shape using tmap
问题描述
我制作了一张地图,不使用 tmap
以包含在使用 leaflet
的闪亮应用程序中.我大致有我想要的:一个基于 SpatialPolygonsDataFrame 的具有填充颜色的专题地图,当您单击地图时,会弹出一个包含有关多边形的额外信息的弹出窗口.我想在单击时更改弹出窗口以获得更好的布局.默认情况下会显示数据集中的名称,但它对用户并不友好.
这是一个可重现的例子.
库(tmap)图书馆(传单)数据(欧洲)tmap_mode("视图")carte <- tm_shape(欧洲) +tm_borders(alpha = 0.5) +tm_fill(col = "well_being",id = "名称",popup.vars = c("life_exp","well_being"))tmap_leaflet(carte)
我试图命名向量(popup.vars = c("Life Expectancy" = "life_exp", "Wellbeing" = "well_being)
,但这不起作用.
我还尝试在调用 leaflet::addPolygons
时添加弹出窗口,但我收到一条错误消息.
carte2 <- tm_shape(Europe) +tm_borders(alpha = 0.5) +tm_fill(col = "well_being")nom <- 欧洲$名称tmap_leaflet(carte2) %>%addPolygons(layerId = nom,popup = paste0("<b>",~name,"</b><br/>预期寿命:",~life_exp," <br/>幸福 : ", ~well_being))
<块引用>
derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolygons") 中的错误:未找到多边形数据;请为 addPolygons 提供数据和/或 lng/lat 参数
谢谢
在开发版本中,popup.vars 的向量名称现在用作标签.此外,我还为每个图层函数添加了 popup.format.您可以分别为每个变量指定数字格式.
数据(世界,地铁)Metro$growth <- (metro$pop2020 - metro$pop2010)/(metro$pop2010 * 10) * 100ttm()tm_shape(地铁)+tm_bubbles("pop2010", col = "growth",边框.col =黑色",边框.alpha = .5,style="fixed",breaks=c(-Inf, seq(0, 6, by=2), Inf),调色板=-RdYlBu",对比度=1,title.size="都会人口",title.col="增长率(%)", id="name",popup.vars=c("人口 (2010)"="pop2010", "人口 (2020)"="pop2020", "增长 (%)"="growth"),popup.format=list(增长=list(digits=4)))
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)
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))
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
Thanks
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屋!