[英]R Shiny Plot Click with geom bar and facets
我想創建一個交互式的情節shiny
和ggplot2
。 我已經能夠使用geom_point
和其他具有明顯x
和y
軸的geoms
成功完成此操作。 但是,當使用諸如geom_bar
類的東西時,這會比較困難,因為您沒有y
變量。
這里和這里都存在一種解決方案,可以從click事件中提取x
變量以進行所需的過濾,但是這些處理都沒有帶有構面的圖。 我想在帶有構面的ggplot
上使用click選項。 我試圖在第一個鏈接上修改代碼,但是並沒有成功。
library(ggplot2)
library(shiny)
ui <- fluidPage(
fluidRow(
plotOutput("plot1", height = 300, width = 300,
click = "plot1_click",
)
),
verbatimTextOutput("x_value"),
verbatimTextOutput("selected_rows")
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(ToothGrowth, aes(supp)) + geom_bar(stat = "count") + facet_wrap(~dose)
})
# Print the name of the x value
output$x_value <- renderPrint({
if (is.null(input$plot1_click$x)) return()
lvls <- levels(ToothGrowth$supp)
lvls[round(input$plot1_click$x)]
})
# Print the rows of the data frame which match the x value
output$selected_rows <- renderPrint({
if (is.null(input$plot1_click$x)) return()
keeprows <- round(input$plot1_click$x) == as.numeric(ToothGrowth$supp)
ToothGrowth[keeprows, ]
})
}
shinyApp(ui, server)
單擊任何條形效果很好,可以過濾到x
值,但包括所有構面的結果。 我只想包含點擊方面的結果。
我嘗試nearPoints
與pannelvar1
使用,但是由於沒有y
變量傳遞給它,因此拋出了錯誤。
有什么想法嗎?
我更改了您的上一個功能,並得到了我認為想要的結果。
output$selected_rows <- renderPrint({
if (is.null(input$plot1_click$x)) return()
panel = input$plot1_click$panelvar1
keeprows <- round(input$plot1_click$x) == as.numeric(ToothGrowth$supp) & ToothGrowth$dose==panel
ToothGrowth[keeprows, ]
})
我不確定嘗試訪問panelvar1會遇到什么錯誤,但是我使用了配置選項options(shiny.trace=TRUE)
來檢查如何訪問面板變量(下面添加了日志消息)。 也許您只是想從錯誤的位置獲取價值
RECV {"method":"update","data":{"plot1_click":{"x":1.1651034873830555,"y":4.268063179119527,"panelvar1":2,"mapping":{"x":"supp","y":null,"panelvar1":"dose"},"domain":{"left":0.4,"right":2.6,"bottom":-0.5,"top":10.5},"range":{"left":213.995433789954,"right":294.520547945205,"bottom":268.013698630137,"top":23.4383561643836},"log":{"x":null,"y":null},".nonce":0.39996409229934216}}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.