There are two main approaches to controlling the tooltip text when using ggplotly():
- Use the
text aesthetic to supply the tooltip text as a character vector, then the tooltip argument in ggplotly() to make sure only this aesthetic is placed in the tooltip
library(plotly)
p <- ggplot(txhousing) +
geom_line(aes(date, median, group = city, text = paste0(city, ", TX")))
# by default ggplotly() will display all aesthetic mappings...
# use `tooltip` to specify which ones you want to show
ggplotly(p, tooltip = "text")
- Use
style() to modify the underlying text attribute directly (warning: this is a more of a hack than a real solution, approach 1 is preferable to this approach).
p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
style(p, text = row.names(mtcars))
For more info, see https://plotly-r.com/controlling-tooltips.html