【问题标题】:add colorFactor palette to mapview object将 colorFactor 调色板添加到 mapview 对象
【发布时间】:2022-11-18 14:34:42
【问题描述】:

目的是为因子值提供固定颜色。我很难将 colorFactor 比例应用于 mapview 对象。但是,调色板似乎与 zcol 不匹配。

我尝试了以下,类似于传单地图。

library(mapview)

colors <- colorFactor(palette = c("Red", "Green", "Blue"),
                      levels = c("Oberfranken","Mittelfranken", "Unterfranken"))

mapview(franconia, zcol = "district",col.regions=colors)

我收到以下错误消息:

1: In col.regions(nregions) :   Some values were outside the color
scale and will be treated as NA

有什么帮助吗?

以下将适用于传单,但不使用 mapview。

franconia %>% leaflet() %>% addTiles() %>% addPolygons(fillColor = ~colors(district))

【问题讨论】:

    标签: r leaflet mapview


    【解决方案1】:

    mapview::mapviewColors 似乎可以解决问题:

    library(mapview)
    
    colors <- mapviewColors(x=franconia,
                            zcol = "district", 
                            colors = c("Red", "Green", "Blue"),
                            at = c("Oberfranken","Mittelfranken", "Unterfranken"))
    
    mapview(franconia, zcol = "district",col.regions = colors)
    

    【讨论】:

      【解决方案2】:

      我无法弄清楚如何使用贝尼的回答来指定我想将哪种颜色应用于地图上的特定要素。

      结果是 colorFactor 返回了一个函数。您需要提供要用于为要素着色的字段:

      library('sf')
      library('leaflet')
      library('mapview')
      # Read in shapefile and keep the first three features
      ncShp <- st_read(system.file("shape/nc.shp", package="sf"))[1:3, ]
      ncShp
      # Simple feature collection with 3 features and 14 fields
      # Geometry type: MULTIPOLYGON
      # Dimension:     XY
      # Bounding box:  xmin: -81.74107 ymin: 36.23388 xmax: -80.43531 ymax: 36.58965
      # Geodetic CRS:  NAD27
      # AREA PERIMETER CNTY_ CNTY_ID      NAME  FIPS FIPSNO CRESS_ID BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79                       geometry
      # 1 0.114     1.442  1825    1825      Ashe 37009  37009        5  1091     1      10  1364     0      19 MULTIPOLYGON (((-81.47276 3...
      # 2 0.061     1.231  1827    1827 Alleghany 37005  37005        3   487     0      10   542     3      12 MULTIPOLYGON (((-81.23989 3...
      # 3 0.143     1.630  1828    1828     Surry 37171  37171       86  3188     5     208  3616     6     260 MULTIPOLYGON (((-80.45634 3...
      
      cols <- c('red', 'green', 'blue')
      
      # Colour our features sequentially based on the NAME field
      ncShp$NAME
      # [1] "Ashe"      "Alleghany" "Surry" 
      # Ashe will be red, Alleghany will be green, Surry will be blue
      
      colPal <- leaflet::colorFactor(palette = cols, levels = ncShp$NAME)
      
      mapview::mapview(ncShp, zcol='NAME', col.regions=colPal(ncShp$NAME))
      

      【讨论】:

        猜你喜欢
        • 2020-05-22
        • 1970-01-01
        • 1970-01-01
        • 2015-02-26
        • 1970-01-01
        • 1970-01-01
        • 2018-05-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多