ToolTip when you mouseover a ggplot on shiny

前端 未结 4 1162
梦毁少年i
梦毁少年i 2020-12-04 18:16

I am building a shiny application.

I am plotting charts using ggplot.

When I mouseover the points on the graph, I want a tooltip showing one of the columns i

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 18:51

    You can also use a little bit JQuery and conditional renderUI to show a custom tooltip near the pointer.

    library(shiny)
    library(ggplot2)
    
    ui <- fluidPage(
    
      tags$head(tags$style('
         #my_tooltip {
          position: absolute;
          width: 300px;
          z-index: 100;
         }
      ')),
      tags$script('
        $(document).ready(function(){
          // id of the plot
          $("#plot1").mousemove(function(e){ 
    
            // ID of uiOutput
            $("#my_tooltip").show();         
            $("#my_tooltip").css({             
              top: (e.pageY + 5) + "px",             
              left: (e.pageX + 5) + "px"         
            });     
          });     
        });
      '),
    
      selectInput("var_y", "Y-Axis", choices = names(mtcars), selected = "disp"),
      plotOutput("plot1", hover = hoverOpts(id = "plot_hover", delay = 0)),
      uiOutput("my_tooltip")
    )
    
    server <- function(input, output) {
    
      data <- reactive({
        mtcars
      })
    
      output$plot1 <- renderPlot({
        req(input$var_y)
        ggplot(data(), aes_string("mpg", input$var_y)) + 
          geom_point(aes(color = factor(cyl)))
      })
    
      output$my_tooltip <- renderUI({
        hover <- input$plot_hover 
        y <- nearPoints(data(), input$plot_hover)[ ,c("mpg", input$var_y)]
        req(nrow(y) != 0)
        verbatimTextOutput("vals")
      })
    
      output$vals <- renderPrint({
        hover <- input$plot_hover 
        y <- nearPoints(data(), input$plot_hover)[ , c("mpg", input$var_y)]
        # y <- nearPoints(data(), input$plot_hover)["wt"]
        req(nrow(y) != 0)
        # y is a data frame and you can freely edit content of the tooltip 
        # with "paste" function 
        y
      })
    }
    shinyApp(ui = ui, server = server)
    

    EDITED:

    After this post I searched internet to see whether it could be done more nicely and found this wonderful custom tooltip for ggplot. I believe it can hardly be done better than that.

提交回复
热议问题