Formatting mouse over labels in plotly when using ggplotly

See the tooltip argument to ggplotly(). For instance, to show only the species name (e.g. virginica for the top right point) on hover:

g <- ggplot(tail(iris), aes(Petal.Length, Sepal.Length, text=Species)) + geom_point()
ggplotly(g, tooltip="text")

Other examples:

ggplotly(g, tooltip="x")             # Petal.Length: 5.7
ggplotly(g, tooltip="Petal.Length")  # Petal.Length: 5.7
ggplotly(g, tooltip=c("x", "y"))

The last example will show the two-line tooltip

Petal.Length: 5.7
Sepal.Length: 6.7

plotly can make use of the line break HTML tag. You can get what your after using the <br> tag for a newline:

g <- ggplot(df, aes(x,y)) + 
       geom_point(aes(text=sprintf("letter: %s<br>Letter: %s", a, b)))

(gg <- ggplotly(g))

Tags:

R

Ggplot2

Plotly