help with ggtips package (tooltips in Shiny)

I'm trying to get tooltips working with a package called ggtips.
(Found here)

I've done my best to read the documentation and play around (mirroring the working demo file), but I cannot get seemingly simple tooltips to work.

Here is a basic example:

library(tidyverse)
library(ggtips)
library(shiny)

test <- tibble(name = c("bob", "anna", "musa"),
               duration = c(14, 22, 19)
               )

ui <- fluidPage(
  
  # Application title
  titlePanel("ggtips test"),

  mainPanel(
    uiOutput("timePlot")
  )
)

server <- function(input, output) {
  
  output$timePlot <- renderUI({
    
    server <- function(input, output) {
  
  output$timePlot <- renderUI({
    
    plot = ggplot(data = test) + 
      aes(
        x = name,
        y = duration,
        colour = name,
        fill = name) +
      geom_boxplot()
    
    varDict = (structure("Name", names = "name"))
    
    ggtips::plotWithTooltips(plot, varDict = varDict)
  })}
    
# Run the application 
shinyApp(ui = ui, server = server)

The above varDict mirrors the included demo, but I've also tried the code below, which follows the documentation:


server <- function(input, output) {
  
  output$timePlot <- renderUI({
    
    plot = ggplot(data = test) + 
      aes(
        x = name,
        y = duration,
        colour = name,
        fill = name) +
      geom_boxplot()

    ggtips::plotWithTooltips(plot, varDict = list(name = "Name"))
  })}

Any help is greatly appreciated.

Hi there,

I had a look at that package and I would simply not recommend it (unless you have a very specific case). If you run my example below you will see it is a lot easier to get really great custom tooltips to work with plotly.

That package also seems to have some issues with installing. It is also not on cran and you can't be sure going ahead if they will maintain it.

library(plotly)
library(tidyverse)

data(mtcars)
cars <- mtcars
p <- plot_ly(cars, x=cars$wt, y=cars$mpg, 
             mode="markers", color=cars$hp, size=cars$qsec) %>%
  layout(xaxis = list(title = "Weight (1000 lbs)"),
         yaxis = list(title = "miles per gallon") )

p

Created on 2021-11-05 by the reprex package (v2.0.0)

I concur with a lot of what you said.

I had originally done this with Plotly, but it adds things other than the tooltip (including things like Plotly branding, which maybe can be removed), so I found this as a more direct alternative. I was hoping it would be easy to implement and give just the tooltip feature I need, but... well, here we are. :slight_smile:

Have a look here as well on tooltip customisation: 25 Controlling tooltips | Interactive web-based data visualization with R, plotly, and shiny

If you don't want to use plotly then I recommend ggiraph

library(ggiraph)
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5

gg_point_1 <- ggplot(
  data = mtcars,
  mapping = aes(
    x = disp, y = qsec,
    color = wt, tooltip = row.names(mtcars), data_id = row.names(mtcars)
  )
) +
  geom_point_interactive(size = 2) +
  scale_color_gradient(low = "#F3C899", high = "#8C120A")

x <- girafe(
  ggobj = gg_point_1, width_svg = 6, height_svg = 6,
  options = list(
    opts_sizing(rescale = FALSE),
    opts_tooltip(
      opacity = .8,
      css = "background-color:gray;color:white;padding:2px;border-radius:2px;"
    ),
    opts_hover(css = "fill:#1279BF;stroke:#1279BF;cursor:pointer;")
  )
)
x

Created on 2021-11-05 by the reprex package (v2.0.0)

Have a look here as well: Make ggplot2 Graphics Interactive • ggiraph package

Thanks; I will give that a try.

For what it's worth, as laid out here, the issue is that this package only supports tooltip labels for geom_point(): https://github.com/Roche/ggtips/issues/23#issuecomment-528714840

I converted my plot to geom_point and it works perfectly.

That said, I think I will work more with other options as well!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.