snaut
snaut

Reputation: 2535

Show tooltip for only one layer in ggplot2 and plotly

Is it possible to show tooltips for only one layer in ggplot + plotly, even if multiple layers share one aestetic. In this example I would like to show only the blue tooltip at the blue line from the geom_smooth layer but don't show the black tooltip for each point.

library(tidyverse)
library(plotly)
library(palmerpenguins)

gg <- ggplot(penguins, aes(x=flipper_length_mm, y=body_mass_g)) + 
  geom_point() + 
  geom_smooth(se = FALSE)

ggplotly(gg, tooltip=c("y")) %>%
  layout(hovermode = "x unified")

Upvotes: 4

Views: 718

Answers (1)

Gorka
Gorka

Reputation: 2071

You can suppress the tooltip on black dots using the style function:

ggplotly(gg, tooltip=c("y")) %>%
  layout(hovermode = "x unified") %>%
  style(hoverinfo = "skip", traces = 1)

For more examples, see chapter Controlling Tooltips of the book Interactive web-based data visualization with R, plotly, and shiny.

Upvotes: 3

Related Questions