R 闪亮的 ggplot 笔刷

R Shiny ggplot brush

如何使画笔工作并将所选点突出显示为红色。

看来brushedPoints功能没有正常工作。

library(shiny)
library(ggplot2)


server <- function(input, session, output) {

  D = reactive({
    brushedPoints(mtcars,input$brush_1, allRows = TRUE)
  })

  output$Plot = renderPlot({
    set.seed(1)
    X = D()
    X[,"cyl"] = as.character(X[,"cyl"])

    ggplot(X,aes_string(x="cyl",y="mpg")) + 
      geom_boxplot(outlier.shape = NA) + 
      geom_jitter(aes(color = selected_))+
      scale_color_manual(values = c("black","red"),guide=FALSE)

  })

  output$log = renderPrint({
    input$brush_1
  })

  output$Data = renderPrint({
    D()
  })
}

ui <- fluidPage(
  plotOutput("Plot",brush = "brush_1"),
  verbatimTextOutput("Data"),
  verbatimTextOutput("log")

)

shinyApp(ui = ui, server = server)

问题存在是因为您在 brushedPointsggplot 中使用了不同的数据: 将列转换为字符

您可以尝试在brushedPoints

之前编辑数据
library(shiny)
library(ggplot2)


server <- function(input, session, output) {
  mt=mtcars
  mt[,"cyl"] = as.character(mt[,"cyl"])
  D=reactive({brushedPoints(mt,brush = input$brush_1,  allRows = TRUE)})

  output$Plot = renderPlot({
    set.seed(1)
      X <- D()
     ggplot(X,aes_string(x="cyl",y="mpg")) + 
      geom_boxplot(outlier.shape = NA) + 
      geom_jitter(aes(color = selected_))+
      scale_color_manual(values = c("black","red"),guide=FALSE)

  })

  output$log = renderPrint({
    input$brush_1
  })

  output$Data = renderPrint({
    D()
  })
}

ui <- fluidPage(
  plotOutput("Plot",brush = "brush_1"),
  verbatimTextOutput("Data"),
  verbatimTextOutput("log")

)

shinyApp(ui = ui, server = server)

PS

对于箱线图笔刷工作不简单,因为所有 x 坐标 - 离散你可以 select 只有当 select 框中间(点不在实际位置但移动到左侧或对 - 因为 geom_jitter)

说的here没办法用geom_jitter刷