R Shiny應用程序中的宣傳單地圖圖例未顯示顏色 - Leaflet map legend in R Shiny app has doesn't show colors -开发者知识库

R Shiny應用程序中的宣傳單地圖圖例未顯示顏色 - Leaflet map legend in R Shiny app has doesn't show colors -开发者知识库,第1张

When I try to add a legend to a leaflet map for a leaflet map (using the Leaflet for R package) incorporated into a Shiny app, the legend does not show the colors of the color palette. Instead it only shows the colors specified for the NA values, in this case, white.

當我嘗試將圖例添加到傳單應用程序中的傳單地圖(使用Leaflet for R包)的傳單地圖時,圖例不會顯示調色板的顏色。相反,它僅顯示為NA值指定的顏色,在本例中為白色。

R Shiny應用程序中的宣傳單地圖圖例未顯示顏色 - Leaflet map legend in R Shiny app has doesn't show colors -开发者知识库,legend without colors,第2张

The app does the following:

該應用程序執行以下操作:

  • First, it filters a set of data based on user inputs
  • 首先,它根據用戶輸入過濾一組數據

  • Then it generates a choropleth map from the filtered data
  • 然后它從過濾后的數據中生成一個等值區域圖

This is the code I used to make the legend:

這是我用來制作圖例的代碼:

addLegend(position = "bottomleft",
   pal = pal, values = shp.data()$stat.selected,
   title = "Legend",
   opacity = .5)

Where pal is a quantile color palette as follows

其中pal是分位數調色板,如下所示

pal <-colorQuantile(c("#B2FF66","#66CC00","#4C9900","#336600","#193300"),
                    NULL, n = 5, na.color="#FFFFFF")

shp.data() is a reactive expression that is a shapefile filtered based on user inputs and stat_selected is the specific statistic that the user selects for mapping onto colors.

shp.data()是一個反應式表達式,它是一個基於用戶輸入過濾的shapefile,stat_selected是用戶選擇映射到顏色的特定統計信息。

I get the following warnings:

我收到以下警告:

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'

I initially tried to make the legend following the example on the leaflet for R page and used the argument values = ~stat.selected for the addLegend function, but I got this error:

我最初嘗試按照R頁面的傳單上的示例制作圖例,並使用參數values = ~stat.selected作為addLegend函數,但是我收到此錯誤:

Error in UseMethod("doResolveFormula") : 
  no applicable method for 'doResolveFormula' applied to an object of class "NULL"

3 个解决方案

#1


8  

Earlier I had just a simple snippet that showed how to add legends. I did not use the ~ before the legend values as is the norm. I did the traditional dataframe$column and it works nicely.

之前我只有一個簡單的片段,展示了如何添加傳說。我沒有使用傳說值之前的〜作為常態。我做了傳統的dataframe $列,效果很好。

This is now updated to see how it all fits together. Here is a full-fledged mapping run after creating all of the variable cuts, etc. The final cleansed data frame was called zipData

現在更新以查看它們如何組合在一起。在創建所有變量切割等之后,這是一個完整的映射運行。最終清理的數據框稱為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

A portion of the map is shown below.

地圖的一部分如下所示。

R Shiny應用程序中的宣傳單地圖圖例未顯示顏色 - Leaflet map legend in R Shiny app has doesn't show colors -开发者知识库,This shows the layer control and the construction legend,第3张

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复