r - R Shiny 应用程序中的传单 map 图例不显示颜色

标签 r shiny leaflet

当我尝试为包含在 Shiny 应用程序中的传单 map (使用 Leaflet for R 包)向传单 map 添加图例时,图例不显示调色板的颜色。相反,它只显示为 NA 值指定的颜色,在本例中为白色。

legend without colors

该应用程序执行以下操作:

  • 首先,它根据用户输入过滤一组数据
  • 然后它从过滤后的数据中生成一个等值线图

  • 这是我用来制作图例的代码:
    addLegend(position = "bottomleft",
       pal = pal, values = shp.data()$stat.selected,
       title = "Legend",
       opacity = .5)
    

    哪里pal是分位数调色板,如下所示
    pal <-colorQuantile(c("#B2FF66","#66CC00","#4C9900","#336600","#193300"),
                        NULL, n = 5, na.color="#FFFFFF")
    
    shp.data()是一个响应式(Reactive)表达式,它是一个基于用户输入和 stat_selected 过滤的 shapefile是用户选择映射到颜色的特定统计量。

    我收到以下警告:
    Warning in is.na(x) :
      is.na() applied to non-(list or vector) of type 'NULL'
    Warning in is.na(values) :
      is.na() applied to non-(list or vector) of type 'NULL'
    

    我最初尝试按照 R 页面传单上的示例制作图例并使用参数 values = ~stat.selectedaddLegend函数,但我收到此错误:
    Error in UseMethod("doResolveFormula") : 
      no applicable method for 'doResolveFormula' applied to an object of class "NULL"
    

    最佳答案

    早些时候,我只有一个简单的片段,展示了如何添加图例。我没有在图例值之前使用 ~ 作为规范。我做了传统的 dataframe$column 并且效果很好。

    现在已更新以查看它们如何组合在一起。这是在创建所有变量切割等之后的完整映射运行。最终清理的数据框称为 zipData

    # create a full popup
    # add some HTML for editing the styles
    
    zipData$popUp <- paste('<strong>',zipData$Street, '</strong><br>',
                           'TIV = $',prettyNum(zipData$tiv, big.mark = ',',preserve.width = 'none'), '<br>',
                           'City: ', zipData$city, '<br>',
                           'YrBuilt = ', zipData$YearBuilt, '<br>',
                           'Construction = ', zipData$ConstructionCode, '<br>',
                           'Occupancy = ', zipData$OccupancyCode, '<br>',
                           'Premium = $' , prettyNum(zipData$Premium, big.mark = ',',preserve.width = 'none') , '<br>',
                           'GrossArea = ', prettyNum(zipData$GrossArea, big.mark = ',', preserve.width = 'none'), '<br>', 
                           'RoofYr = ', zipData$RoofYearBuilt, '<br>')
    
    # set color scale for key factor
    colorsConst <- colorFactor(rainbow(4), zipData$ConstructionCode)
    
    # color scales for numerical bins
    colorstivValue <- colorFactor(palette = 'Accent', zipData$tivValueLvl)
    colorsYrBuilt <- colorFactor(palette = 'Spectral', zipData$yrBuiltLvl)
    colorsRoofYrBuilt <- colorFactor(palette = "YlOrRd", zipData$roofYrBuiltLvl)
    
    
    # begin the leaflet map construction
    # create the map opbject
    
    m <- leaflet() %>%
        addTiles() %>%
    
    # add different tiles for different color schemes
    
        addProviderTiles(providers$OpenStreetMap, group = 'Open SM')  %>%
        addProviderTiles(providers$Stamen.Toner, group = 'Toner')  %>%
        addProviderTiles(providers$CartoDB.Positron, group = 'CartoDB')  %>%
        addProviderTiles(providers$Esri.NatGeoWorldMap, group = 'NG World') %>%
        setView(lng = -90, lat = 30, zoom = 10) %>%
    
    ##############################
    
        # this section is for plotting the variables
        # each variable below is a layer in the map
    
        # construction
        addCircleMarkers(data = zipData, lat = ~Lat, lng = ~Lon,
                         color = ~colorsConst(ConstructionCode), popup = zipData$popUp,
                         radius = 5, group = 'Construction') %>%
        # tiv
        addCircleMarkers(data = zipData, lat = ~Lat, lng = ~Lon, 
                         color = ~colorstivValue(tivLvl), popup = zipData$popUp,
                         radius = ~tiv/20000, group = 'Bldg Value') %>%
    
        # year built  
        addCircleMarkers(data = zipData, lat = ~Lat, lng = ~Lon, 
                         color = ~colorsYrBuilt(yrBuiltLvl), popup = zipData$popUp,
                         radius = ~YearBuilt/250, group = 'Yr Built') %>%
    
    
    ######################################
    
        # layer control
    
        addLayersControl(
            baseGroups = c('Open SM', 'Toner', 'Carto DB', 'NG World'),
    
            overlayGroups = c('Construction',
                              'TIV',
                              'Yr Built'
            ),
            options = layersControlOptions(collapsed = F)
        ) %>%
    
    
    #################################################       
    add the legends for each of the variables
    
    
        # construction        
        addLegend('bottomright', pal = colorsConst, values = zipData$ConstructionCode,
                  title = 'Construction Code',
                  opacity = 1) %>%
    
         # tiv 
        addLegend('bottomleft', pal = colorstivValue, values = zipData$tivLvl,
                  title = 'TIV',
                  opacity = 1) %>%
    
        # year built
        addLegend('topleft', pal = colorsYrBuilt, values = zipData$yrBuiltLvl,
                  title = 'Yr Built',
                  opacity = 1)
    
    
    m  # Print the map
    

    map 的一部分如下所示。

    This shows the layer control and the construction legend

    关于r - R Shiny 应用程序中的传单 map 图例不显示颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30654397/

    相关文章:

    r - 为什么我收到错误 : argument of length 0

    leaflet - 如何使用角度传单指令和 geojson 更改传单弹出窗口的偏移量?

    r - 如何使用 rgl 绘制虚线/虚线

    r - 根据不同数据集中的值格式化 Shiny 数据表 (DT) 的颜色

    r - R Shiny 中盒子的高度

    r - 在侧边栏菜单项之一上方添加空间

    r - ggplot2 中的 scale_fill - 为 ggplots 列表中的每个图制作渐变颜色

    r - 热删除某些部分的悬停文本 plotly

    leaflet - 确定传单中多个多边形的中心点

    javascript - 创建网络 map 应用程序。 Leaflet 还是 OpenLayers?