R Shiny ggplot画笔

时间:2016-09-21 07:16:50

标签: r ggplot2 shiny 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)

1 个答案:

答案 0 :(得分:1)

存在问题是因为您在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坐标 - 离散你只能选择它的中间框(点不在实际位置但向左或向右移动 - 因为geom_jitter)

如告诉here,无法使用geom_jitter和刷