3

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)


1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.