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?

drmariod
  • 11,106
  • 16
  • 64
  • 110

3 Answers3

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
Jon Olav Vik
  • 1,421
  • 2
  • 12
  • 20
  • 3
    Tooltip for the win. – Nick Oct 05 '17 at 10:27
  • Thanks, Jon. That answer helped to fix my issue. Have a nice day. – Scott Grammilo Apr 09 '18 at 02: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? – Andrew McCartney May 28 '20 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(). – Jon Olav Vik May 28 '20 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 04 '21 at 18:23
  • @Ricky "text" is not really an aesthetic; perhaps you're thinking about "label"? You can use the label aesthetic yet have tooltips: ggplotly(ggplot(tail(iris), aes(Petal.Length, Sepal.Length, label=Species, text=Sepal.Length)) + geom_text(), tooltip="text") – Jon Olav Vik Nov 11 '21 at 11:59
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))
Jota
  • 17,281
  • 7
  • 63
  • 93
  • 2
    Awesome, I didn't thought about that... This makes *even* so much more fun possible!!! – drmariod Jan 05 '16 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 – Wakan Tanka Oct 06 '17 at 08: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. – user1420372 Feb 19 '18 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") – Alperen Taciroglu Nov 05 '20 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))
MokeEire
  • 638
  • 1
  • 8
  • 19