plotly / plotly/plotly.R

Using Plotly event data with a key on a ggplotly object

未关闭
#2,325 1 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
R
星标
2.7k
派生
641
PR 合并指标
30 天内没有已合并 PR

描述

I am using Plotly in an R Shiny dashboard with event data so a user can click on a visualization and the linked table will filter to show just that data observation. In the reprex below, I have a plot made in ggplot that I converted to a Plotly object using ggplotly. This works fine, but when identifying observations using pointNumber in the event data, it misidentifies the observation that was clicked on because the plot and the table have different numbers of observations.

Because of this, I'm trying to identify observations using a "key" argument, but no matter how I define the key, it does not appear as a column in the event data. When this plot is made using Plotly to begin with, eliminating ggplot entirely, it works fine and the key column does appear. However, the visualization I need to use is a phylogenetic tree that has proven impossible to visualize in Plotly, which is why I started it off in ggplot. I have searched extensively, but I can't find a way to make event data work with a key using a ggplot -> plotly object. Is there a way to make this work?

### Load data
data(mtcars)
mtcars$index <- c(1:32)
data <- mtcars


# Define UI
ui <- fluidPage(
  titlePanel("Dashboard"),
  tags$style('.container-fluid {
                             background-color: #e3f2fd;
              }'),
  fluidRow(
    
    column(6,
           h3("Scatterplot"),
           plotlyOutput("phylo_tree")),
    
    # Metadata
    column(12, h3("Metadata")),
    column(12, DT::dataTableOutput("data_table"))
    
  )
)

# Define server logic
server <- function(input, output) {
  
  
  output$phylo_tree <- renderPlotly({
    
    # Create a scatterplot using ggplot2
    scatterplot <- ggplot(mtcars, aes(x = mpg, y = hp, label = rownames(mtcars))) +
      geom_point() +
      geom_text(nudge_y = 5, nudge_x = 1, check_overlap = TRUE) +
      labs(title = "Scatterplot of mtcars Dataset",
           x = "Miles Per Gallon (mpg)",
           y = "Horsepower (hp)") +
      theme_minimal()
    
    scatterplot <- plotly::ggplotly(scatterplot, tooltip = "text", 
                                   source = "source1", key = ~index)
    
    # Collect data when a user clicks on a point
    event_register(scatterplot, event = "plotly_selected")
    
    
    scatterplot
  })
  
  
  output$data_table <- DT::renderDataTable({
    
    # Use click event data
    click_data <- event_data("plotly_click", source = "source1")
    
    observe({
      print(event_data("plotly_click", source = "source1"))
    })
    
    # Check if any points are clicked
    if (!is.null(click_data)) {
      # Filter data based on the clicked point
      data <- data[data$index %in% click_data$key,]

    }
    
    datatable(data, 
              rownames = FALSE, 
              filter = "top",
              extensions = "Buttons",  # Add download buttons
              options = list(scrollX = TRUE, dom = "Bfrtip", 
                             buttons = c("csv", "excel", "pdf"),
                             lengthMenu = list(c(10,25,50,-1),
                                               c(10,25,50,"All"))),
              selection = list(mode = "multiple", target = "row")) %>%
      formatStyle(names(data),lineHeight='80%') 
  })
  
  
}


Edit: I solved my own problem. The key needs to be specified in the aes() argument of the ggplot code. The following ggplot code should work with the above datatable code. Note that the id variable specified here should be some unique key identifier.

# Create a scatterplot using ggplot2
    scatterplot <- ggplot(mtcars, aes(x = mpg, y = hp, label = rownames(mtcars), key = id)) +
      geom_point() +
      geom_text(nudge_y = 5, nudge_x = 1, check_overlap = TRUE) +
      labs(title = "Scatterplot of mtcars Dataset",
           x = "Miles Per Gallon (mpg)",
           y = "Horsepower (hp)") +
      theme_minimal()

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

复现所提供的 R Shiny reprex,重点关注 ggplotly 调用以及 plotly_click 的 event_data 路径。完成的标准是:在 ggplot aes() 中定义的唯一键存在于事件数据中,并能正确过滤表格;issue 的编辑内容报告一种可行的方法。

由索引模型根据 Issue 内容生成。

评估

技术栈
r
领域
data-visualization
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。