r - 编辑 tmap/传单/闪亮的默认值(弹出窗口和磁贴)



我正在使用tmap,传单和闪亮创建巴斯克地区(西班牙)的交互式地图。我已经能够创建地图,但我无法编辑 renderLeaflet 的默认行为,特别是:

  1. 我想编辑默认标签和弹出窗口。理想情况下,我不想弹出窗口(单击时),只希望标签(鼠标悬停时);标注将包含弹出窗口中当前显示的信息,以及一些附加信息,并显示在地图的右上角而不是该区域(类似于这个 https://leafletjs.com/examples/choropleth/)

  2. 我想移除传单图块,以便仅显示 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!
})

然后,您可以通过将colorfill参数传递给addPolygons来更改形状的颜色;您可以分别使用popuplabel参数更改弹出窗口和标签。

在图例(以及多边形颜色)中,您需要提供调色板和值

所有详细信息都在上面的链接上 - 它与您提供的传单链接中的分区统计相同,但使用 R 构建。

相关内容

  • 没有找到相关文章

最新更新