【问题标题】:Shiny Interactive Graph plot showing row names显示行名的闪亮交互式图表
【发布时间】:2017-09-25 14:29:46
【问题描述】:

我将 Shiny 和 ggplot2 用于交互式图形。我还使用“plot1_click”来获取 x 和 y 位置。

output$selected <- renderText({
paste0("Value=", input$plot1_click$x, "\n",
       "Names=", input$plot1_click$y)  }) #how to get names???

这是服务器代码的一部分。这里我想要的是,不是打印“y”坐标,而是打印写在y轴上的相应名称。有什么可行的办法吗??

【问题讨论】:

  • 这是一种解决方法,但假设您用来绘制的数据框包含 x 和 y 坐标,您可以使用类似"Names=", data$name[data$x == input$plot1_click$x &amp; data$y == input$plot1_click$y ]

标签: r shiny interactive


【解决方案1】:

据我所知,plotOutput 不支持点击点。点击事件只会返回点击位置的坐标。然而,这些坐标可用于找出最近的点。

来自闪亮画廊页面的This shiny app 使用函数shiny::nearPoints 正是这样做的。这是一个最小的例子。

library(shiny)
library(ggplot2)

shinyApp(
  fluidPage(
    plotOutput("plot", click = "plot_click"),
    verbatimTextOutput('print')
  ),
  server = function(input, output, session){
    output$plot <- renderPlot({ggplot(mtcars, aes(wt, mpg)) + geom_point()})
    output$print = renderPrint({
      nearPoints(
        mtcars,             # the plotting data
        input$plot_click,   # input variable to get the x/y coordinates from
        maxpoints = 1,      # only show the single nearest point 
        threshold = 1000    # basically a search radius. set this big enough 
                            # to show at least one point per click
      )
    })
  }
)

verbatimTextOutput 显示离点击位置最近的点。请注意,nearPoints 仅适用于这样的 ggplots。但是帮助页面建议还有一种方法可以将其与基本图形一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2020-04-16
    • 2015-12-06
    • 2021-10-14
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多