0

I would like to highlight points in an R plot_ly scatter based on whether the key column matches one of the elements of a query column of the data-point hovered-over by the user. I know you can set up non-reciprocal, hierarchical highlighting between points by populating the key field with lists: points will be highlighted if the elements in their key field form a subset of the elements of the selected point's key field. But I would like to highlight in a more flexible way.

This is a modified example from the r plotly page (down the bottom) using list-columns as keys:

  d <- tibble::tibble(
      x = 1:4, 
      y = 1:4,
      key = list('A','B','C','D'),
      query = list(c('A','C'), 'B',c('A','D'), c('A','B','D'))
    )
    
    highlight_key(d, ~ query) %>%
       plot_ly(x = ~x, y = ~y, hoverinfo = "key") %>%
       highlight("plotly_selected", color = "red") %>%
       highlight(on = "plotly_hover", color = "red", selectize = F, opacityDim = 0.8)

'query' is a column of lists. I'd like points to be highlighted if their 'key' value is a subset of the elements in the list in the 'query' field of the point being hovered over. However right now they are only highlighted if their 'query' list is a subset of 'query' list of the selected point. e.g. I want hovering over point D to highlight points A, B and D, from searching the 'key' column with the elements of the 'query' column. But right now it doesn't highlight A because the query value for A {'A','C'} is not a subset of the query list for D {'A','B','D'}.

Stu2
  • 1
  • 2
  • To your sentence `I know you can do this if the key and query columns are the same:...`, how do you know that it is possible within the `crosstalk` tools? `SharedData` documentation says explains Key to be "Character vector or one-sided formula that indicates the name of the column that represents the key or ID of the data frame.:" And I believe that here this Key is the same as `highlight_key(d, ~ query)$key()`. Then it is not meant to be used as a dictionary for matching key with value in another variable. – KM_83 Oct 07 '20 at 00:42
  • That sentence was ambiguous, I've clarified it. I do not know whether what I am asking is possible with the crosstalk tools. – Stu2 Oct 07 '20 at 13:13

0 Answers0