0

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")

enter image description here

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?

Dan
  • 11,370
  • 4
  • 43
  • 68
  • To the best of my knowledge, plotly's hover text appears only over specified data. In this case, the line is not data - just a visual connecting two data points. The same things happens with geom_line plots – Lost Aug 20 '18 at 19:19
  • 1
    have you seen this : https://stackoverflow.com/questions/38955568/line-segments-or-rectangles-with-hover-information-in-r-plotly-figure ? – MLavoie Aug 21 '18 at 16:24
  • @MLavoie I hadn't seen that – thanks for the link. – Dan Aug 22 '18 at 11:04

0 Answers0