简体   繁体   中英

Can you get rid of the trace labels in the annotations when using ggplotly (ggplot2 with plotly)?

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.

盒子

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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