我是 Shiny 的新手。我想制作一个 Shiny 的应用程序,显示不同参数的空间分布。我使用了 meuse 包中的 sp 数据集。

这是我使用的代码

library(sp)
library(rgdal)
library(ggmap)
library(leaflet)
library(dplyr)
library(shiny)

ui <- fluidPage(
  mainPanel(
  titlePanel("Copper (ppm)"),
  leafletOutput("copper"),
  titlePanel("Lead (ppm)"),
  leafletOutput("lead"),
  titlePanel("Zinc (ppm)"),
  leafletOutput("zinc")
   )
  )
server <- function(input,output){
  output$copper <- renderLeaflet({

data(meuse)
coordinates(meuse) <- 1:2
proj4string(meuse) <- CRS("+init=epsg:28992")
meuse <- spTransform(meuse, CRS("+proj=longlat +datum=WGS84"))
meuse_df <- as.data.frame(meuse)

leaflet(meuse_df) %>%
  addProviderTiles("OpenStreetMap", group = "OpenStreetMap") %>%
  addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
  addCircleMarkers(~x,
                   ~y,
                   radius = ~copper/10,
                   stroke = FALSE,
                   fillOpacity = 0.4,
                   group = "meuse_df",
                   popup = ~as.character(copper)) %>%
addLayersControl(position = "bottomleft",
                 baseGroups = c("OpenStreetMap",
                            "Esri.WorldImagery"),
overlayGroups = "meuse_df")
})
  output$lead <- renderLeaflet({

  leaflet(meuse_df) %>%
  addProviderTiles("OpenStreetMap", group = "OpenStreetMap") %>%
  addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
  addCircleMarkers(~x,
                   ~y,
                   radius = ~lead/50,
                   stroke = FALSE,
                   fillOpacity = 0.4,
                   group = "meuse_df",
                   popup = ~as.character(lead)) %>%
  addLayersControl(position = "bottomleft",
                   baseGroups = c("OpenStreetMap",
                                  "Esri.WorldImagery"),
                   overlayGroups = "meuse_df")
})

output$zinc <- renderLeaflet({

leaflet(meuse_df) %>%
  addProviderTiles("OpenStreetMap", group = "OpenStreetMap") %>%
  addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
  addCircleMarkers(~x,
                   ~y,
                   radius = ~zinc/100,
                   stroke = FALSE,
                   fillOpacity = 0.4,
                   group = "meuse_df",
                   popup = ~as.character(zinc)) %>%
  addLayersControl(position = "bottomleft",
                   baseGroups = c("OpenStreetMap",
                                  "Esri.WorldImagery"),
                   overlayGroups = "meuse_df")
})
}
shinyApp(ui = ui, server = server)

这是我得到的结果

r - Shiny:如何在同一张传单 map 上绘制多个参数?-LMLPHP

我想知道是否有一种方法可以在一张 map 上绘制所有参数(铜、铅和锌)。任何建议,将不胜感激。

更新

感谢@Symbolix 的回答和使用 checkBoxGroupInput 的建议。相反,我使用了 addCircleMarkers 三次,所以我可以在一张 map 上绘制所有金属,我可以打开和关闭它们
ui <- fluidPage(
mainPanel(
titlePanel("All metals (ppm)"),
leafletOutput("metals")
)
)
server <- function(input,output){
output$metals <- renderLeaflet({

data(meuse)
coordinates(meuse) <- 1:2
proj4string(meuse) <- CRS("+init=epsg:28992")
meuse <- spTransform(meuse, CRS("+proj=longlat +datum=WGS84"))
meuse_df <- as.data.frame(meuse)

leaflet(meuse_df) %>%
  addProviderTiles("OpenStreetMap", group = "OpenStreetMap") %>%
  addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
  addCircleMarkers(~x,
                   ~y,
                   radius = ~copper/10,
                   color ="red",
                   stroke = FALSE,
                   fillOpacity = 0.4,
                   group = "copper",
                   popup = ~as.character(copper)) %>%
  addCircleMarkers(~x,
                   ~y,
                   radius = ~lead/50,
                   color ="gren",
                   stroke = FALSE,
                   fillOpacity = 0.4,
                   group = "lead",
                   popup = ~as.character(lead)) %>%
  addCircleMarkers(~x,
                   ~y,
                   radius = ~zinc/100,
                   color ="blue",
                   stroke = FALSE,
                   fillOpacity = 0.4,
                   group = "zinc",
                   popup = ~as.character(zinc)) %>%
  addLayersControl(position = "bottomleft",
                   baseGroups = c("OpenStreetMap",
                                  "Esri.WorldImagery"),
                   overlayGroups = c("copper",
                                     "lead",
                                     "zinc"))
})
}
shinyApp(ui = ui, server = server)

最佳答案

熔化您的数据,使您的金属位于一列中,您就可以开始使用了。

在这里,我使用 library(reshape2) 进行融合。

library(sp)
library(rgdal)
library(ggmap)
library(leaflet)
library(dplyr)
library(shiny)
library(reshape2)

ui <- fluidPage(
    mainPanel(
        titlePanel("Metals"),
        leafletOutput("all_metals")
    )
)
server <- function(input,output){

    output$all_metals <- renderLeaflet({

        data(meuse)
        coordinates(meuse) <- 1:2
        proj4string(meuse) <- CRS("+init=epsg:28992")
        meuse <- spTransform(meuse, CRS("+proj=longlat +datum=WGS84"))
        meuse_df <- as.data.frame(meuse)

        ## melt df so 'metals' are in one column
        ## using 'reshape2' library
        meuse_melt <- melt(meuse_df, measure.vars = c("copper","lead","zinc"), variable.name = "metal")

        ## specify factor levels for colours
        meuse_melt$metal <- factor(sample.int(5L, nrow(meuse_melt), TRUE))
        factpal <- colorFactor(topo.colors(5), meuse_melt$metal)

        ## now you just need one output
        leaflet(meuse_melt) %>%
            addProviderTiles("OpenStreetMap", group = "OpenStreetMap") %>%
            addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %>%
            addCircleMarkers(~x,
                             ~y,
                             radius = ~value/100,
                             stroke = FALSE,
                             fillOpacity = 0.4,
                             group = "meuse_melt",
                             popup = ~metal,
                             color= ~factpal(metal)) %>%
            addLayersControl(position = "bottomleft",
                             baseGroups = c("OpenStreetMap", "Esri.WorldImagery"), overlayGroups = "meuse_melt")
    })

}
shinyApp(ui = ui, server = server)

关于r - Shiny:如何在同一张传单 map 上绘制多个参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35642301/

10-09 18:37