Rshiny:单击多边形时显示图表

Rshiny:单击多边形时显示图表,r,shiny,data-visualization,shinyapps,R,Shiny,Data Visualization,Shinyapps,我是一个非常渴望学习的新手,但现在我面临着一个我无法独自克服的问题,如果有人能帮助我,我将不胜感激!:) 我的问题(我想)很简单: 我用我的多边形创建了一张地图,当我点击它们时,我设法显示了一些基本信息,但我不知道如何在我的地图下面为我点击的每个多边形添加一个条形图(例如) 有人能帮我怎么做吗?(经过数小时的尝试,我的眼珠真的要从眼窝里钻出来了!!!) 非常感谢 罗曼 我的代码: library(shiny) library(leaflet) library(dplyr) library(mag

我是一个非常渴望学习的新手,但现在我面临着一个我无法独自克服的问题,如果有人能帮助我,我将不胜感激!:)

我的问题(我想)很简单:

我用我的多边形创建了一张地图,当我点击它们时,我设法显示了一些基本信息,但我不知道如何在我的地图下面为我点击的每个多边形添加一个条形图(例如)

有人能帮我怎么做吗?(经过数小时的尝试,我的眼珠真的要从眼窝里钻出来了!!!)

非常感谢

罗曼

我的代码:

library(shiny)
library(leaflet)
library(dplyr)
library(magrittr)
library(devtools)
library(RColorBrewer)
library(rgdal)
library(sp)

communes <- readOGR("G:/Ateliers/Projet/communes.shp")
commmunes@data

nom_commune                 INSEE  Variable_1   Variable_2  Variable_3 area_sqkm
1    AUZEVILLE-TOLOSANE     31035         289     8.727212    9.336384  6.979758
2      CASTANET-TOLOSAN     31113          85     4.384877    8.891650  8.460724
3                LABEGE     31254         288     5.047406    2.031651  7.663404
4            PECHBUSQUE     31411         443     6.577743    8.120896  3.099422
5 RAMONVILLE-SAINT-AGNE     31446          95     2.601305    8.909278  6.236784
> 




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


  #### SERVEUR R #####


  bins <- c(3,3.5,6,6.5,7,7.5,8,8.5)
  pal <- colorBin("YlOrRd", domain = communes$area_sqkm, bins = bins) 
  labels <- sprintf(
    "<strong>%s</strong><br/>%g km2",
    communes$nom_commun, communes$area_sqkm
  ) %>% lapply(htmltools::HTML)

server <- function(input, output, session) {
  output$mymap<-renderLeaflet(
    leaflet(communes) %>%
      addProviderTiles(providers$Stamen.TonerLite,
                       options = providerTileOptions(noWrap = TRUE)
      ) %>%
      setView(1.50, 43.54, zoom = 12) %>%
      addTiles()  %>% 
      addPolygons(fillColor = ~pal(area_sqkm),
                  weight = 2,
                  opacity = 1,
                  color = "white",
                  dashArray = "3",
                  fillOpacity = 0.7,
                  highlight = highlightOptions(
                    weight = 5,
                    color = "#666",
                    dashArray = "",
                    fillOpacity = 0.7,
                    bringToFront = TRUE),
                  label = labels,
                  labelOptions = labelOptions(
                    style = list("font-weight" = "normal", padding = "3px 8px"),
                    textsize = "15px",
                    direction = "auto")) %>% 
      addLegend(pal = pal, values = ~area_sqkm, opacity = 0.7, title = NULL,
                position = "bottomright")
  )
}     


shinyApp(ui = ui, server=server)
库(闪亮)
图书馆(单张)
图书馆(dplyr)
图书馆(magrittr)
图书馆(devtools)
图书馆(RColorBrewer)
图书馆(rgdal)
图书馆(sp)
公社
用户界面%lapply(htmltools::HTML)
服务器%
setView(1.50,43.54,缩放=12)%>%
addTiles()%>%
addPolygons(fillColor=~pal(面积平方公里),
重量=2,
不透明度=1,
color=“白色”,
dashArray=“3”,
fillOpacity=0.7,
highlight=highlightOptions(
重量=5,
color=“#666”,
dashArray=“”,
fillOpacity=0.7,
bringToFront=真),
标签=标签,
标签选项=标签选项(
样式=列表(“font-weight”=“normal”,padding=“3px 8px”),
textsize=“15px”,
direction=“auto”))%%>%
addLegend(pal=pal,值=~area_sqkm,不透明度=0.7,标题=NULL,
position=“bottomright”)
)
}     
shinyApp(用户界面=用户界面,服务器=服务器)
我想在条形图中显示的数据是变量1、2和3:

data <- read.csv("G:/Ateliers/Projet/communes.csv", sep=";")
data

nom_commune                 INSEE  Variable_1   Variable_2  Variable_3 area_sqkm
1    AUZEVILLE-TOLOSANE     31035         289     8.727212    9.336384  6.979758
2      CASTANET-TOLOSAN     31113          85     4.384877    8.891650  8.460724
3                LABEGE     31254         288     5.047406    2.031651  7.663404
4            PECHBUSQUE     31411         443     6.577743    8.120896  3.099422
5 RAMONVILLE-SAINT-AGNE     31446          95     2.601305    8.909278  6.236784
> 
数据

这里是一个包含其他数据的示例应用程序,因为我无法访问地图的形状数据。我相信这可能会做你需要它做的事情,并且可以适应你的需要

我将创建一个
reactiveVal
来存储单击的多边形区域的
id
(此变量存储
input$mymap\u shape\u click$id
)。您在
addPolygons
中使用的数据应具有要引用的
id

在绘图中(或在单独的
reactive
表达式中),可以根据包含
id
reactiveVal
过滤数据

library(shiny)
library(leaflet)
library(rgdal)
library(sf)
library(ggplot2)
library(tidyverse)

arcgis_data = st_read("http://data.phl.opendata.arcgis.com/datasets/bc2b2e8e356742568e43b0128c344d03_0.geojson")

arcgis_data$id <- 1:nrow(arcgis_data)  ## Add an 'id' value to each shape

plot_data <- read.table(text =
"id nom_commune                 INSEE  Variable_1   Variable_2  Variable_3 area_sqkm
1    AUZEVILLE-TOLOSANE     31035         289     8.727212    9.336384  6.979758
2      CASTANET-TOLOSAN     31113          85     4.384877    8.891650  8.460724
3                LABEGE     31254         288     5.047406    2.031651  7.663404
4            PECHBUSQUE     31411         443     6.577743    8.120896  3.099422
5 RAMONVILLE-SAINT-AGNE     31446          95     2.601305    8.909278  6.236784", header = T, stringsAsFactors = F
) 

ui <- fluidPage(
  leafletOutput(outputId = "mymap"),
  plotOutput(outputId = "myplot")
)

server <- function(input, output){

  ## use reactive value to store the id from observing the shape click
  rv <- reactiveVal()

  output$mymap <- renderLeaflet({
    leaflet() %>% 
      addPolygons(data = arcgis_data %>% slice(1:5), layerId = ~id) %>% 
      addProviderTiles("CartoDB.Positron")
  })

  observeEvent(input$mymap_shape_click, {
    rv(input$mymap_shape_click$id)
  })

  ## you can now plot your plot based on the id of region selected
  output$myplot <- renderPlot({
    plot_data %>%
      filter(id == rv()) %>%
      pivot_longer(cols = starts_with("Variable"), names_to = "Variable", values_to = "Value") %>%
      ggplot(aes(x = Variable, y = Value)) +
        geom_col()
  })

}

shinyApp(ui, server)

这是一个包含其他数据的示例应用程序,因为我无法访问地图的形状数据。我相信这可能会做你需要它做的事情,并且可以适应你的需要

我将创建一个
reactiveVal
来存储单击的多边形区域的
id
(此变量存储
input$mymap\u shape\u click$id
)。您在
addPolygons
中使用的数据应具有要引用的
id

在绘图中(或在单独的
reactive
表达式中),可以根据包含
id
reactiveVal
过滤数据

library(shiny)
library(leaflet)
library(rgdal)
library(sf)
library(ggplot2)
library(tidyverse)

arcgis_data = st_read("http://data.phl.opendata.arcgis.com/datasets/bc2b2e8e356742568e43b0128c344d03_0.geojson")

arcgis_data$id <- 1:nrow(arcgis_data)  ## Add an 'id' value to each shape

plot_data <- read.table(text =
"id nom_commune                 INSEE  Variable_1   Variable_2  Variable_3 area_sqkm
1    AUZEVILLE-TOLOSANE     31035         289     8.727212    9.336384  6.979758
2      CASTANET-TOLOSAN     31113          85     4.384877    8.891650  8.460724
3                LABEGE     31254         288     5.047406    2.031651  7.663404
4            PECHBUSQUE     31411         443     6.577743    8.120896  3.099422
5 RAMONVILLE-SAINT-AGNE     31446          95     2.601305    8.909278  6.236784", header = T, stringsAsFactors = F
) 

ui <- fluidPage(
  leafletOutput(outputId = "mymap"),
  plotOutput(outputId = "myplot")
)

server <- function(input, output){

  ## use reactive value to store the id from observing the shape click
  rv <- reactiveVal()

  output$mymap <- renderLeaflet({
    leaflet() %>% 
      addPolygons(data = arcgis_data %>% slice(1:5), layerId = ~id) %>% 
      addProviderTiles("CartoDB.Positron")
  })

  observeEvent(input$mymap_shape_click, {
    rv(input$mymap_shape_click$id)
  })

  ## you can now plot your plot based on the id of region selected
  output$myplot <- renderPlot({
    plot_data %>%
      filter(id == rv()) %>%
      pivot_longer(cols = starts_with("Variable"), names_to = "Variable", values_to = "Value") %>%
      ggplot(aes(x = Variable, y = Value)) +
        geom_col()
  })

}

shinyApp(ui, server)

对不起,文件错了!好的一点是:我已经删除了shapefile中的变量,以便更容易阅读和查看我编辑的答案的底部。这可以在我的设置中正常工作,没有任何错误。希望这是有帮助的!非常感谢你,本,它现在工作得很好!!!该死的,我现在可以安睡了!!:再次感谢你!!!博恩·努维尔!机会来了!谢谢你对不起,文件错了!好的一点是:我已经删除了shapefile中的变量,以便更容易阅读和查看我编辑的答案的底部。这可以在我的设置中正常工作,没有任何错误。希望这是有帮助的!非常感谢你,本,它现在工作得很好!!!该死的,我现在可以安睡了!!:再次感谢你!!!博恩·努维尔!机会来了!谢谢你D