45

I am struggling with text formatting when using ggplotly and the mouse over functionality.

library(plotly)
df <- data.frame(a=letters, b=LETTERS, x=runif(26), y=runif(26))
g <- ggplot(df, aes(x,y)) + geom_point(aes(text=sprintf('letter: %s\nLetter: %s', a, b)))
g
(gg <- ggplotly(g))

I would like to have some formatted text or at least a newline in my mouse over label. Is there a good documentation on how to design this mouse over bubble thing?

3 Answers 3

75

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
6
  • 3
    Tooltip for the win.
    – Nick
    Oct 5, 2017 at 10:27
  • Thanks, Jon. That answer helped to fix my issue. Have a nice day. Apr 9, 2018 at 2:39
  • 2
    is it possible to do this for variables that are not included in the original plot? To hover over a data point and see some/all other variables for that point? May 28, 2020 at 20:50
  • 1
    @AndrewMcCartney That would be Species in my first example. The "text" aesthetic does not really exist and is not used by geom_point(). May 28, 2020 at 20:57
  • @JonOlavVik thank you super helpful. What if you want to add to the "text" aesthetic without overwriting it all-together?
    – Ricky
    Oct 4, 2021 at 18:23
32

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))
4
  • 2
    Awesome, I didn't thought about that... This makes even so much more fun possible!!!
    – drmariod
    Jan 5, 2016 at 17:45
  • 4
    @drmariod this gives me an following error: Error: (converted from warning) Ignoring unknown aesthetics: text ggplot2 is in version 2.2.1 Oct 6, 2017 at 8:15
  • 3
    Using this method in a line chart with multiple lines (differentiated by colour), I had to specifically add an aesthetic for group. i.e. geom_line(aes(x=x, y=y, text="", color=z, group=z)) without text the group=z is not necessary. I then used ggplotly(p, tooltip="text") as per answer by @Jon Olav Vik. Feb 19, 2018 at 22:50
  • 1
    For my case I also needed geom_line() variation. Code below worked: myPlot <- ggplot(data = df, aes(x = x, y = y, label = a, text = b)) + geom_line(aes(x = x, y = y, text = "")) ggplotly(myPlot , tooltip="text") Nov 5, 2020 at 15:01
0

Here's a solution using purrr's map function. It kinda surprised me that it worked but I like it.

I bolded the 'letter:' and 'Letter:' headings. This still prints the x-y coordinates, which you can remove with the argument tooltip in ggplotly().

df <- data.frame(a=letters, b=LETTERS, x=runif(26), y=runif(26))
g <- ggplot(df, aes(x,y)) + 
         geom_point(aes(text=map(paste('<b>letter:</b>', a, '<br>', '<b>Letter:</b>', b), HTML)))
g
(gg <- ggplotly(g))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.