rleafletpalettecolor-palette

specifying colors manually in leaflet bivariate map within shiny app contex


Using the code below, I have created a map within the shiny app context. However, as shown in the picture, the polygons' colors are inconsistent with the legend color scheme. I wonder how they can be consistent preferably by changing the legend color scheme. In the code below, the bi_class variable was defined in 9 categories involving a 3-dimensional quantile of x and y variables (i.e, 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=~paste0(NAME," ","County",":"," ",input$sex_map,",", " ",
    #              input$ProjectID,"=",Age,"%"),
    label = labels,
    labelOptions = labelOptions(
      interactive = TRUE,
      style = list(
        'direction' = 'auto',
        'color' =
          'black',
        'font-family' = 'sans-serif',
        # 'font-style'= 'italic',
        'box-shadow' = '3px 3px rgba(0,0,0,0.25)',
        'font-size' = '14px',
        'border-color' = 'rgba(0,0,0,0.5)'
      )
    ),
    # label=~paste(NAME,"<br>",input$sex_map,
    #              input$ProjectID,"=",Age,"%"),
    
    # label = lapply(labs, htmltools::HTML),
    highlightOptions = highlightOptions(
      #color = "red",
      weight = 2,
      bringToFront = T,
      # color = "#666",
      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))  

})

enter image description here


Solution

  • I think if you declare a function that selects the Green colors like this one should probably work:

    palColFun <- function(colorPalette = "Greens", n = 9){
      pal <- RColorBrewer::brewer.pal(n, colorPalette)
      return(pal)
    }
    

    Then in your code for bivariatechropleth you should add as follows:

    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,
        paletteFunction = palColFun,
        fillOpacity = 0.7,
        color = "grey",
        highlightOptions = leaflet::highlightOptions(color = "orange",
                                                     weight = 2,
                                                     opacity = 1)) %>%
      addTiles(options = tileOptions(opacity = 2))  
    

    Ideally you would link palColFun with the same color you generated for the plots, but given the example above, it is not for me to reproduce the example.

    Hopefully this works.