You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R Shiny中堆叠柱状图分段点击关联对应表格数据的实现咨询

R Shiny 堆叠柱状图点击单个分段显示对应数据实现方案

修改逻辑

  • 放弃手动换算点击坐标的逻辑,改用Shiny内置nearPoints()函数,该函数可自动匹配ggplot点击位置对应的数据集行,天然支持堆叠柱状图的分段识别
  • 仅需要修改服务端的坐标匹配和数据过滤逻辑,UI部分无需调整

完整可运行代码

library(shiny)
library(ggplot2)

ui = shinyUI(fluidPage(
  titlePanel("Competency"),
  fluidRow(
    column(6,
           plotOutput("Competency.Name", click = "plot1_click")
    ),
    column(5,
           br(), br(), br(),
           htmlOutput("x_value"),
           verbatimTextOutput("selected_rows"))),
))

server <- function(input, output) {
  
  report <- structure(list(Competency.Official.Rating = structure(c(1L, 2L, 
                                                                    3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
                                                                    4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
                                                                    5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L
  ), .Label = c("0", "1", "100", "2", "3"), class = "factor"), 
  Competency.Name = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
                                2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
                                5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 
                                8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L), .Label = c("Agile", 
                                                                                                     "Co-creating the future", "Collaboration", "Entrepreneurship", 
                                                                                                     "Feedback", "Impact", "One company", "One voice", "Responsibility", 
                                                                                                     "Simplification"), class = "factor"), Freq = c(2L, 9L, 308L, 
                                                                                                                                                    221L, 95L, 7L, 76L, 310L, 191L, 51L, 2L, 12L, 308L, 193L, 
                                                                                                                                                    120L, 2L, 43L, 310L, 220L, 60L, 2L, 49L, 311L, 211L, 62L, 
                                                                                                                                                    3L, 58L, 310L, 208L, 56L, 4L, 22L, 312L, 182L, 115L, 3L, 
                                                                                                                                                    11L, 310L, 196L, 115L, 2L, 9L, 309L, 161L, 154L, 3L, 38L, 
                                                                                                                                                    309L, 226L, 59L)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                           -50L))
  
  output$Competency.Name <- renderPlot({
    ggplot(report, aes(x = Competency.Name, y = Freq, fill = Competency.Official.Rating, label = Freq)) +
      geom_bar(stat = "identity") +
      geom_text(size = 3, position = position_stack(vjust = 0.5))   
  })
  
  # 打印选中的分类和评分
  output$x_value <- renderText({
    if (is.null(input$plot1_click)) return("")
    # 匹配点击位置对应的单条数据
    selected <- nearPoints(report, input$plot1_click, xvar = "Competency.Name", yvar = "Freq", maxpoints = 1)
    if(nrow(selected) == 0) return("")
    HTML("你选中了分类 <code>", selected$Competency.Name, "</code> 下评分为 <code>", selected$Competency.Official.Rating, "</code> 的分段",
         "<br><br>对应的数据行如下:")
  })
  
  # 打印选中的分段对应的数据
  output$selected_rows <- renderPrint({
    if (is.null(input$plot1_click)) return()
    selected <- nearPoints(report, input$plot1_click, xvar = "Competency.Name", yvar = "Freq", maxpoints = 1)
    selected
  })
}

shinyApp(ui, server)

代码运行后,点击堆叠柱状图的任意分段,右侧会直接展示该分段对应的单条数据,包含能力分类、评分和对应频次。

内容的提问来源于stack exchange,提问作者MariKo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 08:57:03