【问题标题】:How to change Leaflet Shiny app circle color according to another variable?如何根据另一个变量更改 Leaflet Shiny app 圆圈颜色?
【发布时间】:2021-10-23 09:15:09
【问题描述】:

我是 Shiny 的新手。我正在尝试用竞争对手药房的积分和我的药房(Tim's pharmacies)的积分制作一个图层。我希望积分的颜色不同(比赛为红色,我的为绿色)。我想我需要从 leaflet() 中删除 pharmacy 并创建一个新的观察事件,但无法使其正常工作。有两个数据集:第一个是竞争对手药店,第二个是我的。我将它们与 rbind 结合起来,并认为我可以根据二进制编码的my_store 列指定颜色(我的为 1,竞争为 0)?任何帮助将不胜感激。

ID Label Lat Long State
1 Bob's 47.14032 -107.334 Montana
2 Bob's 44.57247 -116.125 Montana
3 Evan's 42.88031 -111.989 Idaho
4 Evan's 42.93041 -112.3654 Idaho
5 Silvia's 42.19124 -112.7645 Idaho
6 Evan's 45.7939 -108.768 Montana
7 John's 46.71677 -106.752 Wyoming
ID Label Lat Long State
1 Tim's 47.22632 -107.774 Montana
2 Tim's 44.67257 -116.135 Montana
3 Tim's 42.88031 -111.779 Idaho
4 Tim's 42.89041 -112.3324 Idaho
5 Tim's 42.19124 -112.7645 Idaho
6 Tim's 45.8539 -108.658 Montana
7 Tim's 46.72887 -106.7542 Wyoming
library(shiny)
library(leaflet)
library(dplyr)
library(leaflet.extras)
# install.packages("leaflet.extras")

pharmacy <- read_excel("~/pharmacy.xlsx")
My_Pharmacy <- read_excel("~/My_Pharmacy.xlsx")

all_stores <- rbind(pharmacy, My_Pharmacy)
all_stores <-
    all_stores %>%
    mutate(my_store = if_else(Label == "Tim's Pharmacy",1,0))

# Define UI 
ui <- fluidPage(

    # Application title
    titlePanel("map"),

 

        # Show a map output
        mainPanel(
           leafletOutput(outputId = "map_pharmacy"),
           selectInput(inputId = "State",
                       label = "choose a store brand",
                       choices = unique(pharmacy$State))
        )
    )


# Define server logic required 
server <- function(input, output, session) {
    filteredData <- reactive({
        pharmacy %>%
            filter(State == input$State)
    })

    output$map_pharmacy <- renderLeaflet({
        leaflet(pharmacy) %>% addTiles() %>%
            fitBounds(~min(Long), ~min(Lat), ~max(Long), ~max(Lat))
        })
    
    
    observe({
    leafletProxy("map_pharmacy", data = filteredData()) %>%
        clearShapes() %>%
        addCircles(color = "red", weight = 10)
    })
}

# Run the application 
shinyApp(ui = ui, server = server)```

【问题讨论】:

    标签: r shiny leaflet


    【解决方案1】:

    我会创建一个调色板pal 用于addCircles 的颜色。

    您可能希望colorFactor 根据您的二元因素(我的药房或不是我的药房)着色。

    您可以使用预定义的调色板(请参阅RColorBrewer::display.brewer.all() 了解可用的不同调色板)。或者,您可以定义自己并专门为每个因子级别选择颜色。

    这是完整的例子:

    library(shiny)
    library(leaflet)
    
    pharmacy$my_store <- 0
    My_Pharmacy$my_store <- 1
    
    all_stores <- rbind(pharmacy, My_Pharmacy)
    
    pal <- colorFactor(
      # Use a predefined palette:
      # palette = "Dark2",
      # 
      # Or specify individual colors:
      palette = c("purple", "orange"),
      domain = all_stores$my_store
    )
    
    ui <- fluidPage(
      titlePanel("map"),
      mainPanel(
        leafletOutput(outputId = "map_pharmacy"),
        selectInput(inputId = "State",
                    label = "choose a store brand",
                    choices = unique(all_stores$State))
      )
    )
    
    server <- function(input, output, session) {
      
      filteredData <- reactive({
        all_stores %>%
          filter(State == input$State)
      })
      
      output$map_pharmacy <- renderLeaflet({
        leaflet(filteredData()) %>% 
          addTiles() %>%
          fitBounds(~min(Long), ~min(Lat), ~max(Long), ~max(Lat))
      })
      
      observe({
        leafletProxy("map_pharmacy", data = filteredData()) %>%
          clearShapes() %>%
          addCircles(color = ~pal(my_store),
                     lng = ~Long,
                     lat = ~Lat,
                     weight = 10)
      })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 谢谢!这就像一个魅力。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    相关资源
    最近更新 更多