3

is it possible to remove the trace labels in the annotations when using ggplotly?

For example:

library(ggplot2)
library(plotly)

g <- ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point() +
  annotate("rect", xmin = 4, xmax = 5, ymin = 4, ymax = 5,
           colour = "MediumSeaGreen", fill = "MediumSeaGreen", alpha = 0.3)

ggplotly(g)

Here, how would I get rid of the 'trace 1' when I hover over the green box? I would like to keep the hover on the points though.

box

Note: Also posted here: https://community.rstudio.com/t/can-you-get-rid-of-the-trace-labels-in-the-annotations-when-using-ggplotly-ggplot2-with-plotly/129754

william3031
  • 1,653
  • 1
  • 18
  • 39

1 Answers1

4

You can try using style and specifying hoverinfo as follows:

ggplotly(g) %>%
  style(hoverinfo = "skip")

Note you can use "none" or "skip" to hide the tooltip with hover. If none or skip are set, no information is displayed upon hovering. But, if none is set, click and hover events are still fired.

You can also indicate which traces you'd like this to apply to. If none are explicitly included, then it will apply to all traces.

To remove the annotation hoverinfo for a specific geom, you can try this:

gg <- ggplotly(g)
gg$x$data[[2]]$hoverinfo <- "none"
gg

Reference: https://plotly.com/r/reference/#layout

Ben
  • 28,684
  • 5
  • 23
  • 45
  • Thanks for the response Ben. However that also gets rid of the hover info on the points. I would like to keep the ones on the points, but remove the one on the dots. I'll update the question. – william3031 Feb 21 '22 at 22:06
  • Actually, I didn't read the last bit properly. Will have more of a loook. – william3031 Feb 21 '22 at 22:07
  • @william3031 See edited answer - does this work for you? – Ben Feb 21 '22 at 22:09