The following code draws a triangle in ggplotly
. I want to use the labels (from my data frame) as tooltips. How can I do this?
library(tibble)
library(dplyr)
library(ggplot2)
library(ggplotly)
data_points <- tibble(Node = c(1,2,3), X = c(1,2,1.5), Y = c(4,5,6))
data_segments <- tibble(From = c(1,2,3), To = c(2,3,1), label = c('Line 1: 800', 'Line 2: 1600', 'Line 3: 400'))
data <- data_segments %>%
left_join(data_points %>% rename(From = Node, X_from = X, Y_from = Y)) %>%
left_join(data_points %>% rename(To = Node, X_to = X, Y_to = Y))
p <- ggplot(data) +
geom_point(aes(x = X_from, y = Y_from)) +
geom_segment(aes(x = X_from, y = Y_from, xend = X_to, yend = Y_to, text = label))
ggplotly(p, tooltip = "text")
Instead of getting this tooltip by hovering over a point, I want to have it on a line.
I've found a few related items on points like this one, but none of them seem to work with lines.
Basically they all use the aesthetic text or label to render the effect. The problem is that geom_segment
and geom_line
don't accept this aesthetic. Is there another way around this issue?
There are others that mention issues with lines, like this one, but that is also not the problem I'm looking for.
Edit: it seems that there are some options with traces which is the plotly equivalent of a line. However, so far I've only found working results with traces where the lines have a lot of corners or bends (e.g. the density plot here). With straight lines, the tooltips still end up on the points.