简体   繁体   English

R ggplot2单击boxplot

[英]R ggplot2 click with boxplot

When I click one point in the chart, that point is highlighted as red. 当我单击图表中的一个点时,该点将突出显示为红色。

But soon it goes back to black. 但很快它又回到了黑色。

Is there any way to hold the selection? 有没有办法保持选择?

library(shiny)
library(ggplot2)


server <- function(input, session, output) {
  mtcars$cyl = as.character(mtcars$cyl)


  D = reactive({
    nearPoints(mtcars, input$click_1,allRows = TRUE)
  })

  output$plot_1 = renderPlot({
    set.seed(123)
    ggplot(D(),aes(x=cyl,y=mpg)) + 
      geom_boxplot(outlier.shape = NA) + 
      geom_jitter(aes(color=selected_),width=0.02,size=4)+
      scale_color_manual(values = c("black","red"),guide=FALSE)

  })

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

ui <- fluidPage(

  plotOutput("plot_1",click = clickOpts("click_1")),
  verbatimTextOutput("info")

)

shinyApp(ui = ui, server = server)

Okay, my approach is slightly different to Valter's: selected points become red, whilst you can deselect them and they turn back to black. 好吧,我的方法与Valter的略有不同:选择的点变为红色,而你可以取消选择它们然后它们变回黑色。

The key to achieve this effect (or even Valter's answer with 1 selected point) is to use reactiveValues to keep track of the selected points. 实现这种效果的关键(甚至是Valter对1个选定点的回答)是使用reactiveValues来跟踪选定的点。

library(shiny)
library(ggplot2)


server <- function(input, session, output) {
  mtcars$cyl = as.character(mtcars$cyl)

  vals <- reactiveValues(clicked = numeric())
  observeEvent(input$click_1, {
    # Selected point/points
    slt <- which(nearPoints(mtcars, input$click_1,allRows = TRUE)$selected)

    # If there are nearby points selected:
    #   add point if it wasn't clicked
    #   remove point if it was clicked earlier
    # Else do nothing

    if(length(slt) > 0){
      remove <- slt %in% vals$clicked
      vals$clicked <- vals$clicked[!vals$clicked %in% slt[remove]]
      vals$clicked <- c(vals$clicked, slt[!remove])
    }
  })

  D = reactive({
    # If row is selected return "Yes", else return "No"
    selected <- ifelse(1:nrow(mtcars) %in% vals$clicked, "Yes", "No")
    cbind(mtcars, selected)
  })

  output$plot_1 = renderPlot({
    set.seed(123)
    ggplot(D(),aes(x=cyl,y=mpg)) + 
      geom_boxplot(outlier.shape = NA) + 
      geom_jitter(aes(color=selected),width=0.02,size=4)+
      scale_color_manual(values = c("black","red"),guide=FALSE)
  })

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

ui <- fluidPage(

  plotOutput("plot_1",click = clickOpts("click_1")),
  verbatimTextOutput("info")

)

shinyApp(ui = ui, server = server)

I am not sure what is the problem but this is the first workaround I have come up to: 我不确定问题是什么,但这是我提出的第一个解决方法:

            library(shiny)
            library(ggplot2)


            server <- function(input, session, output) {
                    mtcars$cyl = as.character(mtcars$cyl)
                    df <- reactiveValues(dfClikced = mtcars)


                    observe({                
                            if (!is.null(input$click_1)) {
                                    df$dfClikced <- nearPoints(mtcars, input$click_1, allRows = TRUE)        
                            }})

                    output$plot_1 = renderPlot({
                            set.seed(123)
                            if (names(df$dfClikced)[NCOL(df$dfClikced)]== "selected_") {

                                    ggplot(df$dfClikced,aes(x=cyl,y=mpg)) + 
                                            geom_boxplot(outlier.shape = NA) + 
                                            geom_jitter(aes(color=selected_),width=0.02,size=4)+
                                            scale_color_manual(values = c("black","red"),guide=FALSE)       
                            } else {
                                    ggplot(df$dfClikced,aes(x=cyl,y=mpg)) + 
                                            geom_boxplot(outlier.shape = NA) + 
                                            geom_jitter(width=0.02,size=4)+
                                            scale_color_manual(values = c("black","red"),guide=FALSE)         
                            } 

                    })

                    output$info = renderPrint({
                            df$dfClikced
                    })
            }

            ui <- fluidPage(

                    plotOutput("plot_1",click = clickOpts("click_1")),
                    verbatimTextOutput("info")

            )

            shinyApp(ui = ui, server = server)

let me know... 让我知道...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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