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.