58

I have a simple data frame:

seq <- 1:10
name <- c(paste0("company",1:10))
value <- c(250,125,50,40,40,30,20,20,10,10)
d <- data.frame(seq,name,value)

And I want to plot it this way:

require(ggplot2)
ggplot(data = d,aes(x=seq,y=value))+geom_line() + geom_point()

Now I want to use plotly, mostly to be able, when mousing over a point, to get other information than the value, such as the company name. I try this :

require(plotly)
ggplotly()

which get me a tooltip, but with only seq and value. I tried the option tooltip= but it's specified you can use the only variable describe in the aesthetic, and I don't use the name in my aes.

Any solution? I saw I am not the first with this problem, but I haven't found answer working with ggplotly.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Malta
  • 1,883
  • 3
  • 17
  • 30

4 Answers4

82

You don't need to modify the plotly object as suggested by @royr2. Just add label = name as third aesthetic

ggplot(data = d, aes(x = seq, y = value, label = name)) + geom_line() + geom_point()

and the tooltip will display name in addition to seq and value.

The ggplotly help file says about tooltip parameter:

The default, "all", means show all the aesthetic mappings (including the unofficial "text" aesthetic).

So you can use the label aesthetic as long as you don't want to use it for geom_text.

BTW: I've also tried text instead of label

ggplot(data = d, aes(x = seq, y = value, text = name)) + geom_line() + geom_point()

but then ggplot2 complained

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

and plotted only points. I had to add a dummy group to geom_line to remove the issue:

ggplot(data = d, aes(x = seq, y = value, text = name)) + geom_line(group = 1) + geom_point()

(But note if you put the dummy group as fourth aesthetic inside aes() it will appear by default also in the tooltip.)

However, I find the unofficial text aesthetic can become useful alongside label if you want to have different strings plotted by geom_text and shown in the tooltip.

Edit to answer a question in comments: The tooltip parameter to ggplotly() can be used to control the appearance. ggplotly(tooltip = NULL) will suppress tooltips at all. ggplotly(tooltip = c("label")) selects the aesthetics to include in the tooltip.

Uwe
  • 41,420
  • 11
  • 90
  • 134
  • thank you, this seems to me to be a more readable way of obtaining what I want. If someone want to use geom_text, then aes should be put directly in geom_line and in geom_text. – Malta Apr 01 '16 at 07:03
  • 1
    Is it possible to also remove the default labels in addition to adding others? – Jack H Aug 10 '17 at 15:31
  • @VisionIncision Yes. I've updated my answer accordingly. – Uwe Aug 10 '17 at 15:56
  • @UweBlock Thank you for your assistance thus far. Sorry I should have been clearer, for some reason with the way I am creating a plot (possibly because I am doing it with shiny), it does not seem to make a difference if I use any tooltips arguments. So I was wondering if there was something I could do in the aes() function, like how you can add them with label=, label2= etc to remove default labels. – Jack H Aug 15 '17 at 17:05
  • @JackH I struggled with this for a while. Remember to use the name of the *aesthetic*, **not** your variable, in the call to tooltip. For example, use `"x"` not `"seq"` and `"label"` not `"name"`. – Nova Apr 05 '18 at 13:53
  • Can you please take a look at this question? https://stackoverflow.com/questions/66166106/r-error-first-argument-data-must-be-a-data-frame-or-shared-data thanks! – stats_noob Feb 12 '21 at 04:04
  • 1
    I found that if you use `label = name` the tooltip info will show `name: companyX`, whereas if you use `text = name` the tooltip will just show `companyX`. This was useful for me because I had already created the tooltip info in it's own column using `str_c('Line 1: ', line1_col, '
    Line 2: ', line2_col', ...)` and when using `label = ` it annoyingly included the name of the column at the beginning of the label.
    – hugh-allan Apr 27 '22 at 05:33
59

Building on @UweBlock's answer, you can also create dummy aesthetics in order to display multiple labels in tooltips. I can't find where this is documented, but discovered it emperically. The dummy variables show up in the order you specify them, but priority is given to the default variables (e.g. x and y). To get around this, you can specify those variables in a separate aesthetic, as shown below:

library(plotly)
p = ggplot(iris, aes(label=Species, label2=Petal.Length, label3=Petal.Width)) + 
  geom_point(aes(Sepal.Length,Sepal.Width))
ggplotly(p)

enter image description here

teadotjay
  • 1,395
  • 12
  • 15
  • 2
    This really works. I have a quick question, is there any way to order the tooltip? I mean x and y axis will be first and then the other labels? – Joy Jul 28 '16 at 16:25
  • @Joy it seems to want to put x and y first, unless you move them to the geom_point's aes: `p = ggplot(iris, aes(label=Species, label2=Petal.Length, label3=Petal.Width)) + geom_point(aes(Sepal.Length,Sepal.Width))`. The resulting plot will show the tooltips in the order they appear in the command. – teadotjay Jul 29 '16 at 00:31
  • Thank you for your response but I am getting error: argument 3 is not a vector. Any idea? – Joy Jul 29 '16 at 09:41
  • Not sure; I pasted that line into R (version 3.2.5) and got the expected plot. I'm using plotly version 3.4.13 and ggplot2 version 2.1.0. See my updated answer for the full script. – teadotjay Jul 29 '16 at 13:44
  • 1
    this is still working in R v4.0.3. I really wish I could up vote this post twice because it's an awesome trick – Nate Mar 05 '21 at 01:16
39

The unofficial text aesthetic allows you to introduce all the variables you want (here I use name twice to show it):

require(ggplot2)
ggplot(data = d,aes(x = seq, 
                    y = value,
                    group = 1,
                    text = paste('name: ', name,
                                 '</br>name_again: ', name)
                    ))+
  geom_line() + 
  geom_point()

I have to add a dummy group aesthetic for the geom_line to work properly as @UweBlock suggested.

At last I choose what I want to show in the tooltip (here I excluded group).

require(plotly)
ggplotly(, tooltip = c("x", "y", "text"))
DAVL
  • 866
  • 13
  • 8
15

You'll have to modify the plotly object to do this. Or use plot_ly() to create the graph instead...

EDIT: With the release of plotly 4.0 the syntax will change a little bit.

seq <- 1:10
name <- c(paste0("company",1:10))
value <- c(250,125,50,40,40,30,20,20,10,10)
d <- data.frame(seq,name,value)

require(plotly)
gg <- ggplot(data = d,aes(x=seq,y=value))+geom_line() + geom_point()

gg <- plotly_build(gg)

#OLD:
gg$data[[1]]$text <- paste("Seq:", d$seq, "<br>",
                           "Value:", d$value, "<br>",
                           "Company:", d$name)

#UPDATED: 
#Plotly_build creates two separate traces:
#One with mode = markers and the other with mode = lines
#Hence modify text for the second trace

gg$x$data[[2]]$text <- paste("Seq:", d$seq, "<br>",
                           "Value:", d$value, "<br>",
                           "Company:", d$name)

gg
royr2
  • 2,239
  • 15
  • 20
  • great, it works thank you ! I will give plot_ly() a look too, I imagine this can give more readable code, but this way I am not compulsed to update all my code ! – Malta Mar 31 '16 at 07:15
  • 1
    This solution works for me locally but when I place the app on shiny server the additional tooltip is not there. Then I tried the solution below and that works. – Joy Jul 28 '16 at 16:24
  • @royr2 still no dice -"company" does not appear in tooltip – Cyrus Mohammadian Aug 03 '16 at 08:33
  • @CyrusMohammadian seems okay for me - I am using `plotly v4.0.1` – royr2 Aug 03 '16 at 09:49
  • @royr2 interesting, I'm on version 3.6.0, I just reinstalled it from CRAN and it's still that same version, are you installing from github? – Cyrus Mohammadian Aug 03 '16 at 23:11
  • I've updated plotly by installing it from github, it works now for the example above but when used with a ggplot choropleth map (geom_polygons), it adds an additional tooltip box and keeps the previous one as well...very odd behavior – Cyrus Mohammadian Aug 04 '16 at 00:11
  • How to make it work with facets present? It works only in the 1st facet for me. – mattek Mar 05 '17 at 15:54
  • you need to run through all of them. So you can make a for loop (lets say with i in....) where you change gg$data[[i]]$text. Seems in plotly 4.5.6.9000 you need to change gg$x$data instead of gg$data. – Jan Stanstrup Mar 17 '17 at 09:44