Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show tooltip for only one layer in ggplot2 and plotly

Tags:

r

ggplot2

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")
like image 738
snaut Avatar asked Aug 31 '25 03:08

snaut


1 Answers

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.

like image 187
Gorka Avatar answered Sep 02 '25 18:09

Gorka