The code below creates a plotly
figure of a line using geom_segment
with hover text.
# Load libraries
library(ggplot2)
library(plotly)
# Create data frame
df <- data.frame(xmin = 0, xmax = 1, y = 1, label = "Hover!")
# Create plot with hover
g <- ggplot(df) + geom_segment(aes(x = xmin, xend = xmax,
y = y, yend = y, text = label))
ggplotly(g, tooltip = "text")
When I hover over either end point of the segment, the text appears as expected.
My question: How do I make the text appear when hovering over any part of the segment? I could create and plot a data frame with lots of points along the segment so that when I hover over the line I'll be also hovering over one of these points, but that's quite cludgy. Is there a better way?