How to Add Hover Text to a ggplot2?

I want to add hover text to my ggplot graphic. I have tried plotly but it doesn't work with geom_xsidehistogram which is critical to this plot. Does anyone know of any other packages that might have the same tooltip/hover text ability that work with geom_tile and geom_xsidehistogram?

Thanks in advance

Edit:
I should clarify the issue I am having with plotly is that it removes my side histogram. I only need the tool tip/hover text to appear over the tile portion of the graph. So any package that shows hover text over geom_tile and doesn't format side histograms weirdly is a win for me.

Hi @katchamp. It looks like the ggiraph package works for geom_tile, but I am unaware of anything that works with geom_xsidehistogram.

I should clarify the issue I am having with plotly is that it removes my side histogram. I only need the tool tip/hover text to appear over the tile portion of the graph. So any package that shows hover text over geom_tile and doesn't format side histograms weirdly is a win.

Unfortunately, when I tried ggiraph I ran into install errors.

Can you provide a reproducible example of your code?

1 Like

Below is an example using ggiraph and the iris data that provides tooltips over the tiles and preserves the side historgrams. What errors are you getting when trying to install?

library(tidyverse)
library(ggside)
library(ggiraph)

p <-ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species, fill = Species)) +
  geom_tile_interactive(aes(tooltip = paste('Sepal length: ', Sepal.Length))) +
  geom_xsidehistogram(binwidth = 0.1) +
  geom_ysidehistogram(binwidth = 0.1)

girafe(ggobj = p, height_svg = 3)

I don't know how helpful code will be in this case because I original problem I was having with plotly was that it literally isn't built to work with geom_xsidehistogram. They even provided a warning saying that they aren't implemented for the geom_GeomXsidebar functions yet. I am literally just looking for suggestions of other packages that have similar functionality, that I can investigate further on my own.

But if you must know my code to answer this question here you go.
My data structure was
master - a data frame containing:

  • region (character)
  • envir_var_elevation (numeric)
  • loggdppc (numeric)
  • value (numeric)
  • reactive_value (numeric)

df_plot - a data frame containing:

  • envir.group (numeric factor)
  • loggdppc.group (numeric factor)
  • value.squish (numeric)
  • reactive_value.mean (numeric)

df_plot was created by taking the data from master binning elevation and loggdppc and then finding the mean in each bin combination.

Here is my plot code

p_elev_income = ggplot(df_plot, aes(x = envir.group, y = loggdppc.group, fill = value.squish,
                                      text = glue("reactive adapt: {reactive_value.mean}"))) + 
    geom_tile() + 
    geom_xsidehistogram(data = master, aes(x = envir_var_elevation, y = after_stat(density)), binwidth = .01, inherit.aes = F) +
    geom_ysidehistogram(data = master, aes(x = after_stat(density), y = loggdppc), binwidth = 0.08, inherit.aes = F) +
    ggside(x.pos = "bottom", y.pos = "left") +
    xlab(xlab_name) +
    ylab("Log GDP per Capita") +
    scale_fill_gradientn(colours = blue_red.colors, 
                         name = scale_name,
                         limits = limits_val,  
                         breaks = breaks_labels_val,
                         labels = breaks_labels_val,
                         values = rescale(rescale_val),
                         na.value = NA
                         ) +
    theme(panel.background = element_rect(fill = 'white', colour = 'grey'),
          ggside.panel.scale.x = 0.2,
          ggside.panel.scale.y = 0.2)

ggplotly(p_elev_income)

This is the way the plot is supposed to look:

This is the way plotly handled it, the hover text is exactly what I need unfortuntely it fails to plot my side histograms:

1 Like

I was getting an Error 1 which seemed to be related to a compilation failure.

The known solutions for this error didn't work for me so I reported an issue to the developer's GitHub so you can read the details of what I experienced there: https://github.com/davidgohel/ggiraph/issues/281

2 Likes

This topic was automatically closed 21 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.