r - 如何根据另一个变量更改 Leaflet Shiny app 圆圈颜色?
问题描述
我是新来的闪亮。我正在尝试用竞争对手药店的积分和我的药房(Tim's pharmacies)的积分制作一层。我希望积分的颜色不同(比赛为红色,我的为绿色)。我以为我需要pharmacy
从传单()中删除并创建一个新的观察事件,但无法使其正常工作。有两个数据集:第一个是竞争对手药房,第二个是我的。我将它们与 rbind 结合起来,并认为我可以根据二进制编码my_store
列指定颜色(1 代表我的,0 代表竞争)?任何帮助,将不胜感激。
ID | 标签 | 纬度 | 长 | 状态 |
---|---|---|---|---|
1 | 鲍勃的 | 47.14032 | -107.334 | 蒙大拿 |
2 | 鲍勃的 | 44.57247 | -116.125 | 蒙大拿 |
3 | 埃文的 | 42.88031 | -111.989 | 爱达荷州 |
4 | 埃文的 | 42.93041 | -112.3654 | 爱达荷州 |
5 | 西尔维娅的 | 42.19124 | -112.7645 | 爱达荷州 |
6 | 埃文的 | 45.7939 | -108.768 | 蒙大拿 |
7 | 约翰的 | 46.71677 | -106.752 | 怀俄明州 |
ID | 标签 | 纬度 | 长 | 状态 |
---|---|---|---|---|
1 | 蒂姆的 | 47.22632 | -107.774 | 蒙大拿 |
2 | 蒂姆的 | 44.67257 | -116.135 | 蒙大拿 |
3 | 蒂姆的 | 42.88031 | -111.779 | 爱达荷州 |
4 | 蒂姆的 | 42.89041 | -112.3324 | 爱达荷州 |
5 | 蒂姆的 | 42.19124 | -112.7645 | 爱达荷州 |
6 | 蒂姆的 | 45.8539 | -108.658 | 蒙大拿 |
7 | 蒂姆的 | 46.72887 | -106.7542 | 怀俄明州 |
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)```
解决方案
I would create a palette pal
to use for colors with addCircles
.
You may want colorFactor
to color based on your binary factor (my pharmacy or not my pharmacy).
You can either use a pre-defined palette (see RColorBrewer::display.brewer.all()
for different palettes available). Or, you can define yourself and specifically select colors for each factor level.
Here is the complete example:
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)
推荐阅读
- ios - iOS / Swift无法将文件写入将文件路径视为目录路径
- php - 使用 PHP 连接到 vCenter REST API
- r - R - 在没有替换的情况下对时间序列中的连续日期序列进行采样?
- typescript - 打字稿条件验证取决于字段可空性
- java - 在 Apache POI 中创建 XSSFWorkbook 时出现 OutOfMemoryError
- java - 是否可以在 JButton 中禁用文本截断?
- amazon-web-services - 将 java 应用程序与 aws 上的 rds 连接
- git - git clone 致命:远程意外挂断 git 2.17 存储库与 git 1.7 客户端
- asp.net-mvc - 记录 .net 核心应用程序的最佳实践
- python - vs code - 这个 python (3.7) 包结构如何在 AWS Lambda 中工作,但在本地 (vs code) 环境中不工作?