【问题标题】:Shiny - legend with animated leaflet闪亮 - 带有动画传单的传奇
【发布时间】:2021-07-15 22:12:11
【问题描述】:

我有一个动画地图,按组对点进行颜色编码(其中组由用户输入提供)。并非所有组都出现在所有时间戳中。我希望图例保持静态 - 即显示用户选择的所有组,而点移动/消失(如果该组/时间不存在)。

我不知道如何使图例正常工作(目前,图例和地图之间的颜色不协调 - 例如,地图上显示的第一个点是“b”,但它的颜色 -编码为“a”,因为我的两个数据集之间的group 值存在差异(points() 存储与动画滑块中显示的日期戳相关的数据,df() 存储组上的数据由用户选择...

任何帮助将不胜感激。下面是一个玩具示例。

library(plyr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(shiny)
library(leaflet)

set.seed(0)         
data <- data.frame(Lon = -119.5, Lat = 49.3, Group = letters[1:10]) %>%
        crossing(Date = seq(as.Date("2020-01-01"), as.Date("2020-01-10"), 1)) %>%
        mutate(Lon = rnorm(n(), Lon, 0.1),
                Lat = rnorm(n(), Lat, 0.1))
data <- data[sample(1:nrow(data), 40),]

ui <- fluidPage(
        sidebarLayout(sidebarPanel(selectInput(inputId = "Var", label = "select", 
                        choices = letters[1:6], multiple = TRUE, selected = c("a", "b", "c"))),
            mainPanel(sliderInput("animationSlider", "Date:", 
                 min = min(data$Date), max = max(data$Date), value = min(data$Date), step = 1,
                 animate = animationOptions(interval = 600, loop = FALSE)),
                        leafletOutput("MapAnimate", width="1100px", height="650px")))) 
                                                
                
server <- function(input, output, session) {
    df <- reactive({
            data %>%
                filter(Group %in% input$Var)
                                })

     points <- reactive({
          req(input$animationSlider)
          df() %>%
            filter(Date == input$animationSlider)
   })
    
     output$MapAnimate <- renderLeaflet({
        df.in <- df()
        pal <- colorFactor("RdYlBu", df.in$Group)

         leaflet(data) %>%
            setView(lng = -119.5, lat = 49.3, zoom = 9) %>%
            addProviderTiles("Esri.WorldImagery", layerId = "basetile") %>%
         addLegend(title = "ID", position = "topleft", pal = pal, values = ~df.in$Group)     
                                 }) 
                                 
 observe({
     df.in <- points()
 
     pal <- colorFactor("RdYlBu", df.in$Group)
 
     leafletProxy("MapAnimate", data = points()) %>%
         clearShapes() %>%
         addCircles(lng = ~Lon, lat = ~Lat, fillOpacity = 1, color = ~pal(df.in$Group), popup = ~Group) 
 })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r animation shiny leaflet


    【解决方案1】:

    一旦您为每个 Group 值修复了颜色,您应该能够获得所需的输出。试试这个

    library(plyr)
    library(dplyr)
    library(tidyr)
    library(ggplot2)
    library(shiny)
    library(leaflet)
    
    set.seed(0)         
    data <- data.frame(Lon = -119.5, Lat = 49.3, Group = letters[1:10]) %>%
      crossing(Date = seq(as.Date("2020-01-01"), as.Date("2020-01-10"), 1)) %>%
      mutate(Lon = rnorm(n(), Lon, 0.1),
             Lat = rnorm(n(), Lat, 0.1))
    data <- data[sample(1:nrow(data), 40),]
    
    ui <- fluidPage(
      sidebarLayout(sidebarPanel(selectInput(inputId = "Var", label = "select", 
                                             choices = letters[1:6], multiple = TRUE, selected = c("a", "b", "c"))),
                    mainPanel(sliderInput("animationSlider", "Date:", 
                                          min = min(data$Date), max = max(data$Date), value = min(data$Date), step = 1,
                                          animate = animationOptions(interval = 600, loop = FALSE)),
                              leafletOutput("MapAnimate", width="1100px", height="650px")))) 
    
    
    server <- function(input, output, session) {
      df <- reactive({
        data %>%
          filter(Group %in% input$Var)
      })
      
      points <- reactive({
        req(input$animationSlider)
        df() %>%
          filter(Date == input$animationSlider)
      })
      
      mycolorlist <- c("red", "blue", "black", "purple", "green", "orange", "yellow", "steelblue", "cyan", "maroon", "darkblue", "darkgreen", "brown")
      n <- length(unique(data$Group))
      mycolors <- reactive({
        colorFactor("RdYlBu", levels=unique(data$Group))
        #colorFactor(mycolorlist[1:n], levels=unique(data$Group))  ## manually define your own colors
      })
      
      output$MapAnimate <- renderLeaflet({
        df.in <- df()
        pal <- mycolors() # colorFactor("RdYlBu", df.in$Group)
        
        leaflet(data) %>%
          setView(lng = -119.5, lat = 49.3, zoom = 9) %>%
          addProviderTiles("Esri.WorldImagery", layerId = "basetile") %>%
          addLegend(title = "ID", position = "topleft", pal = pal, values = ~df.in$Group)     
      }) 
      
      observe({
        df.in <- points()
        
        pal <- mycolors() # colorFactor("RdYlBu", df.in$Group)
        
        leafletProxy("MapAnimate", data = points()) %>%
          clearShapes() %>%
          addCircles(lng = ~Lon, lat = ~Lat, fillOpacity = 1, color = ~pal(df.in$Group), popup = ~Group)
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 采取了一些措施将其完全实现到我的实际应用程序中(显然还有其他一些问题,叹息......),但现在一切都很好。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2015-12-21
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    相关资源
    最近更新 更多