1

I have data with groups. For every group I plot a subplot in plotly. The x-values for all of these subplots correspond to identical description, just the y-axis value is either missing or different across groups. I would like to link these subplots so that if I hover over one point in one subplot, it will also highlight the points with the same x value in other subplots. I have tried the crosstalk package examples, but either I create the subplots wrong or I don't understand how to use it. Does anyone have any experience?

Update:

df <- data.frame(
     ID = rep(1:4, times = 2), 
     Value = rnorm(8),
     group = c(1,1,1,1,2,2,2,2)
 )

subplot(
     plot_ly(df[df$group==1,], x = ~ID, y = ~Value),
     plot_ly(df[df$group==2,], x = ~ID, y = ~Value), nrows = 2
 )

And my question is, how could I highlight the points with the same x value in all subplots if I hover on one of the points?

Liis Kolberg
  • 153
  • 12

1 Answers1

1

With the library crosstalk, you first need to transform your dataframe to a SharedData object to use in several connected plots.

With the function facet_grid(), you can create separate graphs according to the levels of a variable.

library(plotly)
library(crosstalk)
library(tidyverse)

df <- data.frame(
  ID    = rep(1:4, times = 2), 
  Value = rnorm(8), 
  group = c(1, 1, 1, 1, 2, 2, 2, 2)
)

shared_df <- SharedData$new(df)

ggplotly(
  shared_df %>%
    ggplot(aes(x = ID, y = Value)) + 
    geom_point() +
    facet_grid(~ group)
)

In the result, if you click on a point on the left graph, it will also highlight this same point on the right graph.

enter image description here

demarsylvain
  • 2,103
  • 2
  • 14
  • 33
  • Yes, but let's say I have also a group variable and one plot is for one group and other is for another group. Sort of something like the following. But the SharedData does not allow to subset. df <- data.frame( ID = rep(1:4, times = 2), Value = rnorm(8), group = c(1, 1, 1, 1, 2, 2, 2, 2) ) shared_df <- SharedData$new(df) bscols( plot_ly(shared_df[shared_df$group==1,], x = ~ID, y = ~Value), plot_ly(shared_df[shared_df$group==2,], x = ~ID, y = ~Value) ) – Liis Kolberg Feb 06 '19 at 21:38
  • you're right. you need to use ggplotly() and facet_grid() in order to split graph according groups without changing the initial df. I will edited the answer. – demarsylvain Feb 06 '19 at 23:18
  • Thanks, this is much closer to what I try to achieve. However, these two plots in the example are not linked anymore. If I choose a point with ID = 2 on one plot, it will not highlight the point with the same ID in other. I think it is because they have separate crosstalk keys? – Liis Kolberg Feb 07 '19 at 08:22
  • Ah, I got it now:) shared_df <- SharedData$new(df, key = ~ ID) did the trick:) Thank you very much @demarsylvain, you were a great help:) – Liis Kolberg Feb 07 '19 at 08:26
  • There's a typo `<- <-` but the original question was whether multiple tooltips can show on hover, and this answer is about highlighting multiple points on click, so it seems the original question has not been answered? – Brooks Ambrose Apr 27 '22 at 21:13
  • I fixed the typo. About highlighting, when you pass the mouse on a specific point, it will highlight it (and reduce luminosity of others) but it will also do the same in other graphs, and only highlight the point associated with the one you "selected". – demarsylvain May 26 '22 at 16:56