0

I am really puzzled by the changes that occur when I make a simple modification in the code.

What I would like to have is that in an interactive (ggplotly) output the hover over box only displays the actual values of the variables without the variable text/column names. It is best shown by a simple examples where it works and when the unexpected changes occur:

Here the test example:

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)

p1 <- ggplot(data = d,aes(x=seq,y=value)) + geom_line()
ggplotly(p1)  

The output is fine. However, when you hover over the line, the box shows the values and the column names (here: "seq" and "value"). I would like to get rid of "seq" and "value".

I googled and found an easy solution for one variable just by adding the option "text" to the aesthetics.

p1 <- ggplot(data = d,
             aes(x=seq, y=value, text = value  )) + 
  geom_line()
ggplotly(p1,
         tooltip = c("text"))  

So far so good. Now I thought I paste the two figures that should appear in the chart via a simple paste() together.

p1 <- ggplot(data = d,
             aes(x=seq, y=value, text = paste(value, '<br>', seq)  )) + 
  geom_line()
ggplotly(p1,
         tooltip = c("text"))  

However, the lines disappear! The hover over boxes appear here and there but not always.

I could fence the problem a little bit more by just pasting another string to ONE variable. This causes the same effect: the lines disappear because of paste() function. I guess there is some issue between this paste() and the geom_line() but I tried various modifications without success.

p1 <- ggplot(data = d,
             aes(x=seq, y=value, text = paste("hello: ", value  ))) + 
  geom_line()
ggplotly(p1,
         tooltip = c("text"))  

Any ideas or suggestions?

UPDATE after further testing and hint from Stefan:

By adding a dummy group = 1 works to the aesthetics it works:

p1 <- ggplot(data = d,
             aes(x=seq, y=value, 
                 group = 1,
                 text = paste(value, '<br>', seq)  )) + 
  geom_line()
ggplotly(p1,
         tooltip = c("text"))  

Still, it is puzzling to me why the paste() has this impact on the code.

Oliver
  • 31
  • 5
  • You should watch the messages. At least on my machine I got `geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?` Therefore try with adding the `group` aesthetic, i.e. add `group=1` to `aes()` – stefan Jan 26 '21 at 18:11
  • I did not have any messages. And there are no groups defined in the data. The issue does not seem to be whether the observations are in groups or not. Can you re-run my test example above in a new session and empty memory. Would be interesting if your messages still persist. – Oliver Jan 26 '21 at 19:57
  • Hi Oliver. Sorry. Wasn't meant bad. The difference when I ran your code was that I have not save the plot as p1.That's why I got the message. In general ggplot2 makes a best guess on how to group the data. By default it groups by categorical variables. Without text there is no categorical variable and hence one group. Adding text makes text the grouping variable, i.e. you get one datapoint per group and no line is showing up. Therefore you have to override the default grouping by adding group = 1 which tells ggplot that all data belongs to one group. This is a issue which quite often arises. – stefan Jan 26 '21 at 21:28

0 Answers0