Skip to content

distinguish links and nodes in sankeyPlot using event_data #1752

Open
@isubirana

Description

@isubirana

I am building a sankey plot using plotly package with R within a Shiny application.
At some point, I use event_data function which returns an index pointNumber which is an integer ranging from 0 to the number of pairwise nodes relationships.
When performing sankeyPlot in plotly you have to specify the node and link arguments. The problem is that pointNumber provides an index which I cannot distinguish between the node or the link.
I would like the app could identify if the user clicks on a node or on a link.

Here is a toy example. Note that when clicking on node "A", pointNumber returns 0, and the same when clicking on link connecting node "A" and node "B".

library(plotly); library(shiny)
ui <- fluidPage(
  plotlyOutput("plot"),
  verbatimTextOutput("index")
)
server <- function(input, output){
  output$index <- renderPrint({
    event_data("plotly_click", source="sankeyPlot")
  })
  output$plot <- renderPlotly({
    plot_ly(
      source = "sankeyPlot",
      type = "sankey",
      arrangement = "fixed",
      node = list(label = c("A","B","C","D"), hoverinfo="none"),
      link = list(source=c(0,0,1,2), target=c(1,2,3,3), value=c(2,5,2,1), hoverinfo="none")
    )
  })
}
shinyApp(ui, server)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions