Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R.排除异常值的交互式绘图_R_Shiny - Fatal编程技术网

R.排除异常值的交互式绘图

R.排除异常值的交互式绘图,r,shiny,R,Shiny,是否可以通过修改参数来绘制其他参数的至少一个或多个交互图,例如wt与hp或wt与cyl。如果可能,绘制交互式剂量-反应曲线图将非常有用,因为用户可以选择动态删除外围点并绘制曲线拟合。我目前正在测试这种可能性。一旦成功,我们将在这里发布答案。同时,如果有人有建设性的建议来实现这一点,请给出建议。我尝试按照@HubertL的建议绘制多个交互式绘图,结果成功了。我在下面提供的演示代码可能对像我这样的人有用 library(ggplot2) ui <- fluidPage( fluidRo

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

我尝试按照@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)
库(ggplot2)

ui我尝试按照@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)
库(ggplot2)

为什么不同时复制情节和观察者呢?谢谢你的建议。成功了。为什么不把情节和观察者都复制一下呢?谢谢你的建议。成功了。