我正在使用 tmap、传单和闪亮创建巴斯克地区(西班牙)的交互式地图。我已经能够创建地图,但无法编辑 renderLeaflet 的默认行为,具体来说:
我想编辑默认标签和弹出窗口。理想情况下,我不希望弹出窗口(单击时),只希望标签(鼠标悬停时);标签将包含弹出窗口中当前显示的信息以及一些附加信息,并显示在地图的右上角而不是该区域上(类似这样https://leafletjs.com/examples/choropleth/)
我想删除传单图块,以便仅显示 Esri.WorldGrayCanvas,因此,应该没有在图块之间切换的选项。
以下代码显示了我在说什么。所需的形状文件可以在此处下载 ftp://ftp.geo.euskadi.net/cartografia/Limites/ 或此处 https://www.dropbox.com/s/zx3bg5131pomyfy/CB_COMARCAS_5000_ETRS89.shp?dl=0
library(shiny)
library(shinyWidgets)
library(leaflet)
library(rgdal)
library(tmap)
library(tmaptools)
library(dplyr)
setwd(~)
shape <- read_shape(file="CB_COMARCAS_5000_ETRS89.shp") # Load shape file
shape$COMARCA <- as.character(shape$COMARCA)
shape$COMARCA <- c(# Vector with names of the areas
"Alto Deba", "Arratia-Nervión", "Bajo Bidasoa", "Bajo Deba", "Cantábrica Alavesa",
"Donostialdea", "Duranguesado", "Encartaciones", "Estribaciones del Gorbea",
"Gernika-Bermeo", "Goierri", "Gran Bilbao", "Llanada Alavesa", "Markina-Ondarroa",
"Montaña Alavesa", "Plentzia-Mungia", "Rioja Alavesa", "Tolosa", "Urola Costa", "Valles Alaveses")
datawvals$COMARCA <- c(# Vector with names of the areas
"Alto Deba", "Arratia-Nervión", "Bajo Bidasoa", "Bajo Deba", "Cantábrica Alavesa",
"Donostialdea", "Duranguesado", "Encartaciones", "Estribaciones del Gorbea",
"Gernika-Bermeo", "Goierri", "Gran Bilbao", "Llanada Alavesa", "Markina-Ondarroa",
"Montaña Alavesa", "Plentzia-Mungia", "Rioja Alavesa", "Tolosa", "Urola Costa", "Valles Alaveses")
datawvals <- bind_rows(datawvals, datawvals, datawvals)
datawvals$time <- c(rep("2010",20), rep("2014",20), rep("2016",20))
datawvals$var1 <- runif(60)
datawvals$var2 <- runif(60)
datawvals$var3 <- runif(60)
data4map <- left_join(shape, datawvals, by="COMARCA") #join shape with data to be depicted
years <- c(2010,2014,2016)
runApp(list(
ui = fluidPage(
titlePanel("Shiny tmap!"),
sidebarLayout(
sidebarPanel(
selectInput("var", label = "Variable", choices = c("var1", "var2", "var3"), selected = "var1"),
sliderTextInput(
inputId = "time", label = "year",
choices = years, selected = 2010,
grid = FALSE
)
),
mainPanel(
leafletOutput("map", width=600)
)
)
),
server = function(input, output) {
#Subset data based on year
subsetData <- reactive({
new_data <- data4map[data4map$time==input$time, ]
return(new_data)
})
#Different color palettes for diff vars
colores <- reactive({
if(input$var=="var1"){
colores0 <- "Blues"
} else{
colores0 <- "Reds"
}
return(colores0)
})
#Actual plot
output$map = renderLeaflet({
tm <- tm_shape(subsetData()) +
tm_polygons(input$var, palette=colores())
tmap_leaflet(tm)
})
}
))
需要使用
tmap
吗?我认为你可以通过 leaflet
获得你想要的行为 - 看看这里的指南:
https://rstudio.github.io/leaflet/choropleths.html
一个可行的结构是:
output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles("ESRI.WorldGrayCanvas") %>%
addPolygons(data = subset_data(), ...) %>%
addLegend(position = "topright", ...) # Using ... here to mean more stuff needed!
})
然后,您可以通过将
color
和 fill
参数传递给 addPolygons
来更改形状的颜色;您可以分别使用 popup
和 label
参数更改弹出窗口和标签。
在图例中(以及多边形颜色),您需要提供调色板和值
所有详细信息都在上面的链接上 - 它与您提供的 leafletjs 链接中的 choropleth 相同,但使用 R 构建。