我正在使用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)
})
}
))
预先感谢您提供有关如何修改这些默认设置的提示和建议。
答案 0 :(得分:1)
您需要使用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构建。