【问题标题】:R Shiny interactive plotting with exclusion of outliers排除异常值的 R Shiny 交互式绘图
【发布时间】:2017-07-18 13:15:21
【问题描述】:

是否可以通过修改R Shiny interactive example 来绘制至少一个或多个其他参数的交互图,例如 wt 与 hp 或 wt 与 cyl。如果可能的话,绘制交互式剂量反应曲线图将非常有用,因为用户可以选择动态移除异常点并绘制曲线拟合。我目前正在测试这种可能性。一旦成功,将在此处发布答案。同时,如果有人有建设性的建议来实现这一点,请提出建议。

【问题讨论】:

  • 为什么不同时复制情节和观察事件?
  • 感谢您的建议。它奏效了。

标签: r shiny


【解决方案1】:

我尝试按照@HubertL 的建议绘制多个交互式绘图,并且成功了。我正在提供下面的演示代码,可能对像我这样的人有用。

library(ggplot2)


ui <- fluidPage(
  fluidRow(
    column(width = 6,
           plotOutput("plot1", height = 350,
                      click = "plot1_click",
                      brush = brushOpts(
                        id = "plot1_brush"
                      )
           ),
           actionButton("exclude_toggle", "Toggle points"),
           actionButton("exclude_reset", "Reset"),
           plotOutput("plot2", height = 350,
                      click = "plot2_click",
                      brush = brushOpts(
                        id = "plot2_brush"
                      )
           ),
           actionButton("exclude_toggle2", "Toggle points2"),
           actionButton("exclude_reset2", "Reset")
    )
  )
)

server <- function(input, output) {
  # For storing which rows have been excluded
  vals <- reactiveValues(
    keeprows = rep(TRUE, nrow(mtcars)),
    keeprows1 = rep(TRUE, nrow(mtcars))
  )

  output$plot1 <- renderPlot({
    # Plot the kept and excluded points as two separate data sets
    keep    <- mtcars[ vals$keeprows, , drop = FALSE]
    exclude <- mtcars[!vals$keeprows, , drop = FALSE]

    ggplot(keep, aes(wt, mpg)) + geom_point() +
      geom_smooth(method = lm, fullrange = TRUE, color = "black") +
      geom_point(data = exclude, shape = 21, fill = NA, color = "black", alpha = 0.25) 
  })


  output$plot2 <- renderPlot({
    # Plot the kept and excluded points as two separate data sets
    keep    <- mtcars[ vals$keeprows1, , drop = FALSE]
    exclude <- mtcars[!vals$keeprows1, , drop = FALSE]

    ggplot(keep, aes(wt, hp)) + geom_point() +
      geom_smooth(method = lm, fullrange = TRUE, color = "black") +
      geom_point(data = exclude, shape = 21, fill = NA, color = "black", alpha = 0.25) 
  })
  # Toggle points that are clicked on plot 1
  observeEvent(input$plot1_click, {
    res <- nearPoints(mtcars, input$plot1_click, allRows = TRUE)

    vals$keeprows <- xor(vals$keeprows, res$selected_)
  })

  # Toggle points that are brushed, when button is clicked on plot 1
  observeEvent(input$exclude_toggle, {
    res <- brushedPoints(mtcars, input$plot1_brush, allRows = TRUE)
    vals$keeprows <- xor(vals$keeprows, res$selected_)
  })

  # Reset all points for plot 1
  observeEvent(input$exclude_reset, {
    vals$keeprows <- rep(TRUE, nrow(mtcars))
  })

  # Toggle points that are clicked on plot 2
  observeEvent(input$plot2_click, {
    res <- nearPoints(mtcars, input$plot2_click, allRows = TRUE)

    vals$keeprows1 <- xor(vals$keeprows1, res$selected_)
  })

  # Toggle points that are brushed, when button is clicked on plot 2
  observeEvent(input$exclude_toggle2, {
    res <- brushedPoints(mtcars, input$plot2_brush, allRows = TRUE)
    vals$keeprows1 <- xor(vals$keeprows1, res$selected_)
  })

  # Reset all points for plot 2
  observeEvent(input$exclude_reset2, {
    vals$keeprows1 <- rep(TRUE, nrow(mtcars))
  })

}

shinyApp(ui, server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2021-06-16
    • 2014-03-27
    • 1970-01-01
    • 2016-07-12
    • 2019-10-08
    相关资源
    最近更新 更多