[英]R - ggplot: Get the id of a clicked graph
Plotly具有event_data函數,該函數將響應對圖形點的單擊。 但是,您必須為event_data指定一個“源”,以標識要觀看的劇情。
ggplot中是否也提供這種功能。 如果否,那么我們如何找到單擊的圖的ID。
注意:我將wordcloud稱為圖,並且這些圖是動態生成的。
這是一個最小的示例,用於檢查單擊了哪個圖。 每當發生點擊事件時,反應值selected_plot
都會更新。
library(shiny)
ui = fluidPage(
verbatimTextOutput("text"),
plotOutput("plot1", click = "plot1_click"),
plotOutput("plot2", click = "plot2_click")
)
server = function(input, output, session){
output$plot1 = renderPlot({
hist(rnorm(1000))
})
output$plot2 = renderPlot({
plot(cumsum(rnorm(1000)))
})
selected_plot = reactiveVal()
observeEvent(input$plot1_click, {selected_plot(1)})
observeEvent(input$plot2_click, {selected_plot(2)})
output$text = renderPrint({
cat("selected plot:", selected_plot())
})
}
shinyApp(ui, server)
請注意,您還可以指定click
在使用服務器端選項outputArgs
的說法renderPlot
。
沒有任何例子很難解釋。 但是,由於使用ggiraph
軟件包,在使用Shiny
,您可以將geom_point
更改為geom_point_interactive
,然后再使用以下代碼:
selected_dot <- reactive({
input$plot_selected
})
希望能有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.