【问题标题】:Is there a way to conditionally vary opacity of polygons using Leaflet?有没有办法使用 Leaflet 有条件地改变多边形的不透明度?
【发布时间】:2020-07-14 00:57:10
【问题描述】:

我正在使用 R 传单包来绘制加利福尼亚县的数据。我希望每个县的多边形的不透明度根据该县的地块数量而有所不同。地块多的县应该更不透明,地块少的县应该更透明。这可能吗?

我尝试更改 fillOpacity 选项,类似于 fillColor 如何随地块数量变化:

fillOpacity = ~num.parcels

我的数据样本:

packages <- c('dplyr','leaflet','sf','USAboundaries')
lapply(packages, library, character.only = TRUE)

ca_counties <- USAboundaries::us_counties(states = 'CA')

parcels <- structure(list(county = c("Yuba", "Sacramento", "Inyo", "Los Angeles", "Sierra"), 
                          num.parcels = c(27797L, 452890L, 6432L, 15830L, 54291L)), row.names = c(NA, -5L), class = "data.frame")

parcels <- st_as_sf(left_join(parcels, ca_counties[,c('name')], by = c("county" = "name")))

传单地图:

labels <- sprintf(
  "<strong>%s County</strong><br/>
  Parcels: %g<br/>",
  parcels$county, parcels$num.parcels
) %>% lapply(htmltools::HTML)

leaflet(parcels) %>%
  setView(-119, 37.9, 6) %>%
  addTiles() %>%
  addPolygons(
    fillColor = ~pal(num.parcels),
    weight = 2,
    opacity = 1,
    color = 'black',
    dashArray = '2',
    fillOpacity = 0.7,
    highlightOptions = highlightOptions(color = "red", weight = 3,
                                        bringToFront = TRUE),
    label = labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "4px 8px"),
      textsize = "15px",
      direction = 'auto')) %>%
  addLegend(pal = pal, values = ~num.parcels, opacity = 0.7, title = "Number of Parcels",
            position = "bottomleft")

【问题讨论】:

    标签: r leaflet


    【解决方案1】:

    尝试设置fillOpacity = ~num.parcels / max(num.parcels),

    我还包含了一个 pal 函数..您的代码中似乎缺少它..

    # Create a continuous palette function
    pal <- colorNumeric( palette = "Reds", domain = parcels$num.parcels )
    
    #calculate fillopacity
    
    labels <- sprintf(
      "<strong>%s County</strong><br/>
      Parcels: %g<br/>",
      parcels$county, parcels$num.parcels
    ) %>% lapply(htmltools::HTML)
    
    leaflet(parcels) %>%
      setView(-119, 37.9, 6) %>%
      addTiles() %>%
      addPolygons(
        fillColor = ~pal(num.parcels),
        weight = 2,
        opacity = 1,
        color = 'black',
        dashArray = '2',
        fillOpacity = ~num.parcels / max(num.parcels),
        highlightOptions = highlightOptions(color = "red", weight = 3,
                                            bringToFront = TRUE),
        label = labels,
        labelOptions = labelOptions(
          style = list("font-weight" = "normal", padding = "4px 8px"),
          textsize = "15px",
          direction = 'auto')) %>%
      addLegend(pal = pal, values = ~num.parcels, opacity = 1, title = "Number of Parcels",
                position = "bottomleft")
    

    【讨论】:

      猜你喜欢
      • 2015-03-09
      • 1970-01-01
      • 2011-03-12
      • 1970-01-01
      • 2021-10-16
      • 2021-03-01
      • 2017-01-30
      • 2020-09-27
      • 2020-09-08
      相关资源
      最近更新 更多