You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny应用中Leaflet双变量地图图例与多边形颜色匹配问题问询

问题描述

我在Shiny应用中用以下代码制作了一幅地图,但地图上的多边形颜色和图例配色不匹配。我希望让两者颜色一致,优先通过修改图例配色来实现。代码中的bi_class变量是基于x和y变量的3分位数划分的9个类别(low-low、low-medium、low-high、medium-low、medium-medium……)。

output$bi_ACSB_BlackP <- renderLeaflet ({

npal2 <- colorFactor(
  palette =  ("Greens"),
  domain = IDD_nhmap$bi_class
)

labels <- sprintf(
  "<strong>Zip Code=%s </strong> <br/> African American (ACS) = %s <br/> African American (Projects)= %s ",
  IDD_mapdata_()$Zip,
  IDD_mapdata_()$Zip_Black,
  IDD_mapdata_()$Zip_Hisp
) %>%
  lapply(htmltools::HTML)

leaflet (IDD_mapdata_(), options = leafletOptions(zoomSnap = 0.25, zoomDelta =
                                                    0.25)) %>%
  addProviderTiles("CartoDB.Positron",
                   options = providerTileOptions(opacity = 2)) %>%  
  clearControls() %>%
  clearShapes() %>%
  addPolygons(
    fillColor = ~npal2(bi_class),
    stroke = T,
    weight = 1,
    smoothFactor = 0.2,
    fillOpacity = 1,
    color = "black",
    label = labels,
    labelOptions = labelOptions(
      interactive = TRUE,
      style = list(
        'direction' = 'auto',
        'color' =
          'black',
        'font-family' = 'sans-serif',
        'box-shadow' = '3px 3px rgba(0,0,0,0.25)',
        'font-size' = '14px',
        'border-color' = 'rgba(0,0,0,0.5)'
      )
    ),
    highlightOptions = highlightOptions(
      weight = 2,
      bringToFront = T,
      fillOpacity = 0.7
    )
  ) %>%
  
  setView(lng = IDD_mapdata_1()$long,
          lat = IDD_mapdata_1()$lat,
          zoom = 8) %>%

  bivariatechoropleths::addBivariateChoropleth(
    map_data = bivariatechoropleths::renfrew_county,
    var1_name = pop_2016,
    var2_name = median_household_income_2015,
    ntiles= 3,
    var1_label = "African American",
    var2_label = "Hispanics",
    region_name = "CSDNAME",
    weight = 1,
    fillOpacity = 0.7,
    color = "grey",
    highlightOptions = leaflet::highlightOptions(color = "orange",
                                                 weight = 2,
                                                 opacity = 1)) %>%
  addTiles(options = tileOptions(opacity = 2))  
})

地图显示多边形与图例颜色不一致

解决方法

问题出在你同时用自定义的colorFactor(Greens调色板)给多边形上色,又调用addBivariateChoropleth添加默认双变量图例,两者配色逻辑不匹配。以下是两种优先修改图例的解决方案:

方法1:自定义双变量图例匹配Greens调色板

直接生成对应9类bi_class的Greens系颜色,替换默认图例:

  1. 定义匹配的颜色映射:
# 按bi_class的9个类别生成对应Greens深浅的颜色
bi_colors <- c(
  "low-low" = "#f7fcf5", "low-medium" = "#e5f5e0", "low-high" = "#c7e9c0",
  "medium-low" = "#a1d99b", "medium-medium" = "#74c476", "medium-high" = "#41ab5d",
  "high-low" = "#238b45", "high-medium" = "#006d2c", "high-high" = "#00441b"
)
# 更新colorFactor使用该自定义配色
npal2 <- colorFactor(
  palette = bi_colors,
  domain = IDD_nhmap$bi_class
)
  1. 移除原代码中的addBivariateChoropleth调用,替换为自定义图例:
addLegend(
  position = "bottomright",
  colors = bi_colors,
  labels = names(bi_colors),
  title = "African American vs Hispanics",
  opacity = 1
)

方法2:让addBivariateChoropleth使用Greens调色板

让图例和多边形共用同一套双变量Greens配色:

  1. 获取对应3分位数的Greens双变量调色板,用于多边形上色:
bi_palette <- bivariatechoropleths::get_bivariate_palette(ntiles = 3, palette = "Greens")
npal2 <- colorFactor(
  palette = bi_palette,
  domain = IDD_nhmap$bi_class
)
  1. 修改addBivariateChoropleth参数,指定Greens调色板,并替换示例数据为你的真实数据集:
bivariatechoropleths::addBivariateChoropleth(
  map_data = IDD_mapdata_(), # 替换为你的数据集
  var1_name = Zip_Black,     # 替换为生成bi_class的x变量
  var2_name = Zip_Hisp,      # 替换为生成bi_class的y变量
  ntiles= 3,
  var1_label = "African American",
  var2_label = "Hispanics",
  region_name = "Zip",       # 替换为你的区域标识字段
  weight = 1,
  fillOpacity = 0.7,
  color = "grey",
  palette = "Greens",        # 匹配Greens调色板
  highlightOptions = leaflet::highlightOptions(color = "orange",
                                               weight = 2,
                                               opacity = 1))

关键注意事项

  • 必须保证addBivariateChoropleth的map_data、var1_name、var2_name和你生成bi_class的数据集、变量一致,否则图例分类逻辑会和多边形错位。
  • 确认IDD_nhmap$bi_class的类别顺序和调色板的类别顺序完全匹配。

内容的提问来源于stack exchange,提问作者Nader Mehri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 20:25:17