巧妙地识别子图 click_event
Identify Subplots plotly click_event
问题:假设一个情节子情节由多个情节组合而成。除了 curveNumber 和 pointNumber 之外,有没有办法通过 plotly_click 事件来识别子图编号?例如。 (例如下面的代码),p1 被分配了编号 0,p2 被分配了编号 1,p3 被分配了编号 2 我可以使用与 plotly_click event?
类似的命令来获取这些编号
期望输出:
curveNumber pointNumber x y plotNumber
4 1 Top 15 2
代码示例
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)
我认为您不能将 plotNumber
之类的内容添加到 event_data
,但您可以使用 customdata
或 key
属性来传递额外的信息发送至 event_data
.
这是使用 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)
})
}
输出:
curveNumber pointNumber x y customdata
1 4 1 Top 15 p3
问题:假设一个情节子情节由多个情节组合而成。除了 curveNumber 和 pointNumber 之外,有没有办法通过 plotly_click 事件来识别子图编号?例如。 (例如下面的代码),p1 被分配了编号 0,p2 被分配了编号 1,p3 被分配了编号 2 我可以使用与 plotly_click event?
类似的命令来获取这些编号期望输出:
curveNumber pointNumber x y plotNumber
4 1 Top 15 2
代码示例
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)
我认为您不能将 plotNumber
之类的内容添加到 event_data
,但您可以使用 customdata
或 key
属性来传递额外的信息发送至 event_data
.
这是使用 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)
})
}
输出:
curveNumber pointNumber x y customdata
1 4 1 Top 15 p3