使用 Shiny 在传单中的地图图例之间切换

问题描述 投票:0回答:1

我目前正在尝试用闪亮的方式编写传单图例,以便当我在地图之间切换时相应的图例会发生变化。

我已阅读这篇博文:https://medium.com/ibm-data-ai/layer-specific-legend-in-leaflet-8e80ffd72e03,但我对如何将其集成到我的代码中有点迷失.

我使用了形状文件和 DF 来表示全球幸福度和 GDP。

我的数据是名为“世界国家”的形状文件,可以在这里找到:

https://1drv.ms/u/s!AsbNRTohvDRXl7g-JbESdD8Hnc6Azg?e=HvKNAB

我尝试的代码如下:

library(shiny)
library(leaflet)
library(RColorBrewer)

ui <- fluidPage(
  leafletOutput("map")
)

## General Variables
#Label Options
label <- labelOptions( 
  style = list("font-weight" = "normal", padding = "3px 8px"), 
  textsize = "13px", 
  direction = "auto"
)

# Prepare the text labels for tooltips. Selects label name and data it reports
mytext <- paste(
  "Country: ", worldCountries@data$NAME,"<br/>", 
  "Happiness Score: ", round(worldCountries@data$happiness_score, 2), "<br/>", #data rounded to 2 dp
  "GDP: $", round(worldCountries@data$gdp, 2), "<br/>",
  "Population Density: ", round(worldCountries@data$pop_density, 2), "<br/>",
  "Covid Stringency: ", round(worldCountries@data$avg_covid_score, 2), "<br/>",
  sep=""
) %>%
  lapply(htmltools::HTML)



#Highlight Options
highlight <- highlightOptions(
  weight = 2,
  color = "white",
  fillOpacity = 1.0,
  opacity = 1.0,
  bringToFront = TRUE)

## Create Variables for WHI

# Call the color function (colorNumeric) to create a new palette function. 
mypalettewhi <- colorBin( palette="YlOrRd", domain = worldCountries@data$happiness_score)

## Create Variables for gdp

# Call the color function (colorBin) to create a new palette function. Domain allows the colouring of continuous data

mypalettegdp <- colorBin( palette="YlGn", domain=worldCountries@data$gdp)

#Highlight Options
highlightgdp <- highlightOptions(
  weight = 2,
  color = "black",
  fillOpacity = 1.0,
  opacity = 1.0,
  bringToFront = TRUE)



server <- function(input, output, session){
  
  output$map <- renderLeaflet({
    
    leaflet(worldCountries) %>% #creates base map
      addTiles()  %>% 
      setView( lat=10, lng=0 , zoom=2) %>% #sets default map pan
      addPolygons( #code for happiness map
        fillColor = ~mypalettewhi(happiness_score), 
        stroke=TRUE, 
        fillOpacity = 0.9, 
        color="white", 
        weight=0.7,
        label = mytext,
        labelOptions = label,
        highlightOptions = highlight,
        group = "World Happiness Index" #group indicates which toggle pane it belongs to
      ) %>% 
      addLegend( #happiness legend
        values=~happiness_score, 
        opacity=0.9, 
        title = "World Happiness<br /> Index Score", 
        position = "bottomleft",
        colors = c('#ffffb2', '#fed976', '#feb24c', '#fd8d3c', '#fc4e2a', '#e31a1c', '#b10026'),
        labels = c("Less Happy", "", "", "", "", "", "More Happy"),
        group = "World Happiness Index"
      ) %>% 
      addPolygons( #code for gdp map
        fillColor = ~mypalettegdp(gdp), 
        stroke=TRUE, 
        fillOpacity = 0.9, 
        color="black", #black is added as highlight as yellows will blend
        weight=0.7,
        label = mytext,
        labelOptions = label,
        highlightOptions = highlightgdp,
        group = "GDP"
      ) %>% 
      addLayersControl( #controls layers
        baseGroups = c("World Happiness Index", "GDP"),
        options = layersControlOptions(collapsed = FALSE) 
      ) %>% #turns off all groups
      hideGroup(c("World Happiness Index", "GDP")) %>% #this then decides to default show happiness map when loaded
      showGroup("World Happiness Index")
  })
  
}



## update legend when the selected layer group changes
observeEvent(input$map_groups, {
  map <- leafletProxy("map") %>% clearControls()
  
  if (input$map_groups == 'World Happiness Index'){
    map <- map %>%
      addLegend(
        addLegend( #happiness legend
          values=~happiness_score, 
          opacity=0.9, 
          title = "World Happiness<br /> Index Score", 
          position = "bottomleft",
          colors = c('#ffffb2', '#fed976', '#feb24c', '#fd8d3c', '#fc4e2a', '#e31a1c', '#b10026'),
          labels = c("Less Happy", "", "", "", "", "", "More Happy")
        )
      )
  }else{
    map <- map %>%
      addLegend( #code for gdp legend
        values=~gdp, 
        opacity=0.9, 
        title = "GDP (US$)", 
        position = "bottomleft",
        colors = c('#ffffd3', '#d9f0a3', '#addd8e', '#7bce7c', '#41ab5d', '#238443', '#005a32'),
        labels = c("Lower GDP", "", "", "", "", "", "Greater GDP")
      )
  }
})

shinyApp(ui, server)

我的图表目前看起来像这样:

enter image description here

enter image description here

r shiny r-leaflet
1个回答
0
投票

我自己解决了这个问题。问题是我没有正确指定 value =~ 参数中的数据框,例如值=~世界国家$gdp

© www.soinside.com 2019 - 2024. All rights reserved.