plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.57k stars 627 forks source link

Tooltip along the length of a segment #1832

Open julianstanley opened 4 years ago

julianstanley commented 4 years ago

I posted about this here in RStudio community in mid-June but didn't get any responses.

Edit Aug17: Stopped using x-unified hovermode, added new video to show the problem more clearly. Also posted another RStudio community post.

Maybe this feature exists, but I'm missing it? I would like to have a tooltip that extends across the entire length of a segment from add_segments().

Example:

library(plotly)
my_data <- data.frame(
  x = c(1, 6), xend = c(5, 10),
  y = c(1, 2), yend = c(1, 2),
  text = c("First", "Second")
)

plot_ly(my_data, x = ~x, xend = ~xend, y = ~y, yend = ~yend, 
        text = ~text, hoverinfo = "text") %>%
  add_segments()

This will put a tooltip at the beginning and the end of the segment, but users will tend to put their cursor near the middle of the segment and expect a tooltip to appear. Even more importantly, users won't get a tooltip if they are zoomed into the middle of a segment.

demonstration of the problem


I've been getting around this problem by using add_lines() instead, but this is terribly inefficient when I'm trying to plot thousands of these segments at once, and it doesn't solve the zooming problem. E.g.

my_data <- data.frame(
  x = c(seq(1, 5, length.out = 10), NA,
         seq(6, 10, length.out = 10)),
  y = c(rep(1, 10), NA, 
         rep(2, 10)),
  text = c(rep("First", 10), NA,
           rep("Second", 10))
)

plot_ly(my_data, x = ~x,y = ~y,
        text = ~text, hoverinfo = "text") %>%
  add_lines() %>%
  layout(hovermode = "x unified")

demonstration of problem work-around

tdhock commented 4 years ago

sometimes it helps if you tag specific people that you may think can help with the issue @cpsievert ??

cpsievert commented 4 years ago

As far as I'm aware, plotly/plotly.js doesn't really provide a better option than your add_lines() workaround. If the inefficiently is happening at render time, then you may be able to get a speed up from toWebGL()