【问题标题】:Get axis information with event_data使用 event_data 获取轴信息
【发布时间】:2021-08-19 19:07:28
【问题描述】:

这可能不是一个非常高级的问题,这里肯定有答案,但我真的无法在任何地方找到答案。

基本上,单击图块后,我希望将信息显示在图下方的框中。也就是说,我不想要现在显示的 x 和 y 坐标,我想要悬停在图块上时获得的信息 - 单击右上角的图块应该显示信息:“65”、“Toyota Corolla ", "33.9"。

为丑陋的例子道歉。

library(shiny)
library(dplyr)

test <- data.frame(rownames(mtcars),mtcars$mpg,mtcars$hp)

ui <- fluidPage(
  plotlyOutput("bars"),
  verbatimTextOutput("click")
)

server <- function(input, output, session) {
  
  output$bars <- renderPlotly({
    ggplot(data = test, aes(x = rownames.mtcars., y = mtcars.mpg)) +
      geom_tile(aes(fill = mtcars.hp))
  })
  
  output$click <- renderPrint({
    d <- event_data("plotly_click")
    d
  })

}

shinyApp(ui, server)

【问题讨论】:

    标签: r dplyr shiny


    【解决方案1】:

    作为对此的快速回答...

    由于所有“x”值都是唯一的,并且注意到 plotly 沿 x 轴按字母顺序排列了名称,我们可以对列表进行排序并返回行号 x。

    output$click <- renderPrint({
        d <- event_data("plotly_click")
        sortedTest <- test[order(test[["rownames.mtcars."]]),] #alphabetize the data
        sortedTest[d[["x"]],] #return the row that corresponds to the clicked x-value
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 2018-04-10
      • 2012-04-11
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多