Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify Subplots plotly click_event

Question: Suppose a plotly subplot which is combining multiple plots in it. Is there a way to identify the subplot number via plotly_click event besides curveNumber and pointNumber? E.g. (for example code below), p1 gets assigned number 0, p2 gets assigned number 1, p3 gets assigned number 2 and I can get these numbers with a similar command as plotly_click event?

Desired Output:

curveNumber pointNumber   x  y plotNumber
          4           1 Top 15         2

Code Example

library(shiny)
library(plotly)
library(data.table)

dt <- data.table(
  quarter = c("2020 Q1", "2020 Q1", "2019 Q4", "2019 Q4", "2019 Q3", "2019 Q3"),
  type = c("Hop", "Top", "Hop", "Top", "Hop", "Top"),
  count2 = c(3, 4, 5, 6, 0, 1),
  count = c(10, 20, 30, 40, 0, 15)
)

dt[, type := as.factor(type)]

ui <- fluidPage(
  mainPanel(
    plotlyOutput("plot")
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$plot <- renderPlotly({
    p1 <- plot_ly(dt[quarter == "2020 Q1"],
                  x = ~type,
                  y = ~count,
                  source = "plot_y") %>%
      add_bars(y = ~count) %>%
      add_bars(y = ~count2) %>%
      layout(barmode = "stack")

    p2 <- plot_ly(dt[quarter == "2019 Q4"],
                  x = ~type,
                  # y = ~count,
                  source = "plot_y")%>%
      add_bars(y = ~count) %>%
      add_bars(y = ~count2)%>%
      layout(barmode = "stack")

    p3 <- plot_ly(dt[quarter == "2019 Q3"],
                  x = ~type,
                  y = ~count,
                  source = "plot_y")%>%
      add_bars(y = ~count) %>%
      add_bars(y = ~count2)%>%
      layout(barmode = "stack")
list_p <- list(p1, p2, p3)
    subplot(list_p, shareY = TRUE)
  })

  observe({
    click <- event_data("plotly_click", source = "plot_y")
    req(click)
    print(click)
  })
}

# Run the application
shinyApp(ui = ui, server = server)


like image 396
rkraft Avatar asked Sep 12 '26 23:09

rkraft


1 Answers

I don't think you can add something like plotNumber to the event_data, but you can use either the customdata or the key attribute to pass extra information to event_data.

Here is an updated server function using customdata:

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$plot <- renderPlotly({
    p1 <- plot_ly(dt[quarter == "2020 Q1"],
                  x = ~type,
                  y = ~count,
                  customdata='p1',
                  source = "plot_y") %>%
      add_bars(y = ~count) %>%
      add_bars(y = ~count2) %>%
      layout(barmode = "stack")

    p2 <- plot_ly(dt[quarter == "2019 Q4"],
                  x = ~type,
                  customdata='p2',
                  # y = ~count,
                  source = "plot_y")%>%
      add_bars(y = ~count) %>%
      add_bars(y = ~count2)%>%
      layout(barmode = "stack")

    p3 <- plot_ly(dt[quarter == "2019 Q3"],
                  x = ~type,
                  y = ~count,
                  customdata='p3',
                  source = "plot_y")%>%
      add_bars(y = ~count) %>%
      add_bars(y = ~count2)%>%
      layout(barmode = "stack")
    list_p <- list(p1, p2, p3)
    subplot(list_p, shareY = TRUE)
  })

  observe({
    click <- event_data("plotly_click", source = "plot_y")
    req(click)
    print(click)
  })
}

Output:

  curveNumber pointNumber   x  y customdata
1           4           1 Top 15         p3
like image 195
bobbel Avatar answered Sep 14 '26 12:09

bobbel