我想在传单的合页中显示地图数据集的几个功能。
由于我有很多多边形和很多变量,所以我想避免重复图层。
我想根据下面生成的mapview
映射来复制某些内容,但是最好在传单中复制,因为它提供了更大的灵活性并且可以另存为HTML。
我不打算在Shiny中执行此操作,因为目标不是设置网页而是存储HTML(非自包含文件,否则它将下载所有OSM)。
我认为应该可以使用Leaflet的JavaScript功能来做到这一点,但我不确定到底有多精确。
# test example choropleth colour
rm(list = ls())
# pkg ---------------------------------------------------------------------
if(!require(pacman)) install.packages("pacman")
pacman::p_load(dplyr, eurostat, leaflet, sf)
# load maps ---------------------------------------------------------------
map <- eurostat::get_eurostat_geospatial(output_class = "sf") %>%
filter(LEVL_CODE == 0)
map$var1 <- runif(n = 37)
map$var2 <- runif(n = 37)
map$var3 <- runif(n = 37)
# display map -------------------------------------------------------------
m1 <- mapview(map, zcol = "var1")
m2 <- mapview(map, zcol = c("var1", "var2"))
m3 <- mapview(map, zcol = c("var1", "var2", "var3"))
在m1,m2,m3个对象中复制了多边形,请参见对象的大小
palette1 <- colorBin(palette = "Greens", domain = map$var1)
palette2 <- colorBin(palette = "Blues", domain = map$var1)
l1 <- leaflet() %>%
addProviderTiles("OpenStreetMap.HOT") %>%
addPolygons(data = map,
color = "black",
fillColor = ~palette1(var1))
l2 <- leaflet() %>%
addProviderTiles("OpenStreetMap.HOT") %>%
addPolygons(data = map,
color = "black",
fillColor = ~palette1(var1),
group = "var1") %>%
addPolygons(data = map,
color = "black",
fillColor = ~palette2(var2),
group = "var2") %>%
addLayersControl(baseGroups = c("var1", "var2"))
l3 <- leaflet() %>%
addProviderTiles("OpenStreetMap.HOT") %>%
addPolygons(data = map,
color = "black",
fillColor = ~palette1(var1),
group = "var1") %>%
addPolygons(data = map,
color = "black",
fillColor = ~palette2(var2),
group = "var2") %>%
addPolygons(data = map,
color = "black",
fillColor = ~palette1(var3),
group = "var3") %>%
addLayersControl(baseGroups = c("var1", "var2", "var3"))
shapefile在l1,l2,l3对象中重复(请参见对象大小)
m1
l1
请注意,该地图不与eurostat地图一起渲染,而是与我的shapefile一起渲染,因此CRS可能存在一个我不知道的问题。