简体   繁体   English

R Shiny Plot单击带有几何图形栏和刻面的

[英]R Shiny Plot Click with geom bar and facets

I would like to create an interactive plot with shiny and ggplot2 . 我想创建一个交互式的情节shinyggplot2 I have been able to do this successfully with geom_point and other geoms with an obvious x and y axis. 我已经能够使用geom_point和其他具有明显xy轴的geoms成功完成此操作。 When using something like a geom_bar , though, this is harder since you don't have a y variable. 但是,当使用诸如geom_bar类的东西时,这会比较困难,因为您没有y变量。

There is a solution here and here to extract the x variable from the click event to do the filtering desired but neither of these handle plots with facets. 这里这里都存在一种解决方案,可以从click事件中提取x变量以进行所需的过滤,但是这些处理都没有带有构面的图。 I would like to use the click option on a ggplot with facets. 我想在带有构面的ggplot上使用click选项。 I tried to adapt the code at the first link, but this hasn't been successful. 我试图在第一个链接上修改代码,但是并没有成功。

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)

A click on any of the bars does well to filter to the x values but includes results from all facets. 单击任何条形效果很好,可以过滤到x值,但包括所有构面的结果。 I would like to only include results from the clicked facet. 我只想包含点击方面的结果。

在此处输入图片说明

I have tried using nearPoints with pannelvar1 but it throws an error since I don't have a y variable to pass to it. 我尝试nearPointspannelvar1使用,但是由于没有y变量传递给它,因此抛出了错误。

Any thoughts? 有什么想法吗?

I changed your last function and got the result I think you want. 我更改了您的上一个功能,并得到了我认为想要的结果。

  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, ]
  })

在此处输入图片说明

I am not sure what error you were getting from trying to access panelvar1, but I used the configuration option options(shiny.trace=TRUE) to check out how to access the panel variable (log message added below). 我不确定尝试访问panelvar1会遇到什么错误,但是我使用了配置选项options(shiny.trace=TRUE)来检查如何访问面板变量(下面添加了日志消息)。 Perhaps you were just trying to pull the value from the wrong position 也许您只是想从错误的位置获取价值

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM