[英]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.