传单中的空间多边形图顺序

时间:2019-04-03 11:49:13

标签: r leaflet polygon spatial sp

我对小册子中多边形的打印顺序感到烦恼。我想出了一种解决方法,其中涉及将数据子集绘制为单独的多边形,但这并不理想。

是否有一种更简单的方法来调整小叶绘制空间多边形数据框的多边形的顺序?

我已经在这里上传了我的数据集的摘录:https://gofile.io/?c=Ir2aYR

rm(list = ls()); cat("\014"); gc()
pacman::p_load(tidyverse, data.table, leaflet, sp, viridis)


# DATA --------------------------------------------------------------------
catch_dt <- readRDS("I:/stackExample.RDS")
dt = catch_dt@data
dt


# MAPPING -----------------------------------------------------------------
catch_dt_lvl0 = catch_dt %>% subset(nestedLevel == 0)  
catch_dt_lvl1 = catch_dt %>% subset(nestedLevel == 1)
catch_dt_lvl2 = catch_dt %>% subset(nestedLevel == 2)
catch_dt_lvl3 = catch_dt %>% subset(nestedLevel == 3)
catch_dt_lvl4 = catch_dt %>% subset(nestedLevel == 4)

nHotspotType = dt$hotspotType %>% levels %>% length
palHotspotType = colorFactor(viridis(n = nHotspotType), domain = catch_dt$hotspotType)


# Whole dataset
leaflet() %>%

  addProviderTiles(provider = providers$Esri.WorldGrayCanvas) %>% 

  addPolygons(data = catch_dt, 
              weight = 0.2, smoothFactor = 0.2,
              fillColor = ~palHotspotType(catch_dt$hotspotType), fillOpacity = 1.0,
              stroke = FALSE)

enter image description here 这是使用完整数据集时多边形绘制的顺序。请注意,按照正确的顺序绘制时,第二张图的颜色会有所不同。

# Subset
leaflet() %>%

  addProviderTiles(provider = providers$Esri.WorldGrayCanvas) %>% 

  addPolygons(data = catch_dt_lvl0, 
              weight = 0.2, smoothFactor = 0.2,
              fillColor = ~palHotspotType(catch_dt_lvl0$hotspotType), fillOpacity = 1.0,
              stroke = FALSE) %>%

  addPolygons(data = catch_dt_lvl1, 
              weight = 0.2, smoothFactor = 0.2,
              fillColor = ~palHotspotType(catch_dt_lvl1$hotspotType), fillOpacity = 1.0,
              stroke = FALSE) %>%

  addPolygons(data = catch_dt_lvl2, 
              weight = 0.2, smoothFactor = 0.2,
              fillColor = ~palHotspotType(catch_dt_lvl2$hotspotType), fillOpacity = 1.0,
              stroke = FALSE) %>%

  addPolygons(data = catch_dt_lvl3, 
              weight = 0.2, smoothFactor = 0.2,
              fillColor = ~palHotspotType(catch_dt_lvl3$hotspotType), fillOpacity = 1.0,
              stroke = FALSE) %>%

  addPolygons(data = catch_dt_lvl4,
              weight = 0.2, smoothFactor = 0.2,
              fillColor = ~palHotspotType(catch_dt_lvl4$hotspotType), fillOpacity = 1.0,
              stroke = FALSE)

enter image description here

这是所需的图层顺序,其中级别0在底部,级别1在顶部。

0 个答案:

没有答案