Skip to content

Tooltips with ggplotly

Martina Morris edited this page Nov 13, 2021 · 1 revision

You can add interactive hover-based tooltips by pairing ggplot2::ggplot with plotly::ggplotly. The basic workflow is:

p <- ggplot(... aes(..., text = <variable and/or text you want>), ...)
ggplotly(p, tooltip = "text")

Reprex:

df = data.frame(xval=1:10, 
                yval=sample(1:10, 10), 
                zval=sample(c("this", "that"), 10, replace=T))

p <- ggplot(data = df,
            aes(x=xval, y=reorder(yval, xval), 
                text=paste("Group: ", zval))) +
  geom_bar(stat="identity", fill="lightsteelblue") +
  
  labs(title = "Example of tooltip")

ggplotly(p, tooltip = "text")