0

I can't for the life of me figure out why I can't get brushing to work for my two plots (p1,p2) below.

What I'm expecting: If I click the a specific diamond 'cut in the bar graph on the left, the corresponding cut should be highlighted in the scatterplot.

brushing / linking NOT working

Reproducible example below

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

d = ggplot2::diamonds %>% slice_sample(prop = 0.01)

m <- d %>% highlight_key(~cut)
#m = crosstalk::SharedData$new(d, key = ~cut)

p1 = m$data() %>% count(cut) %>% plot_ly() %>% add_bars(x = ~cut, y = ~n, color =~cut)
p2 = m %>% plot_ly() %>% add_markers(x = ~carat, y = ~price, color = ~cut)

subplot(p1,p2) %>% highlight('plotly_click') %>% hide_legend()

This DOES, however, work if both graphs are NON-aggregate graphs.

working

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

d = ggplot2::diamonds %>% slice_sample(prop = 0.01)

m <- d %>% highlight_key(~cut)
#m = crosstalk::SharedData$new(d, key = ~cut)

p1 = m %>% plot_ly() %>% add_markers(x = ~color, y = ~price, color = ~cut)
p2 = m %>% plot_ly() %>% add_markers(x = ~carat, y = ~price, color = ~cut)

subplot(p1,p2) %>% highlight('plotly_click') %>% hide_legend()
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1.3.1056 is probably the version of RStudio, not R. R is currently on v4.0.2 – camille Oct 08 '20 at 14:59
  • One thing weird is that I can't use this chain, which should work `data_frame %>% plot_ly() %>% count(cut)` This results in this error: `Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "c('plotly', 'htmlwidget')"` – ChickenChild77 Oct 08 '20 at 14:59
  • Made a mistake, gave Rstudio version, not R R v4.0.2 plotly v4.9.2.1 , dplyr v1.0.0 – ChickenChild77 Oct 08 '20 at 22:35
  • I don't use plotly often, but doesn't `plot_ly` create a plot? `dplyr::count` is going to expect a data frame, which isn't what you're giving it. Are you positive that's what you saw & what function you're remembering? – camille Oct 08 '20 at 23:04
  • yes, absolutely, I'm sure. Adding the image of the code above. – ChickenChild77 Oct 09 '20 at 11:13
  • **Moderator Note** - Please do not overwrite a particular question that you have asked with a completely different question. [Ask a new question instead.](https://stackoverflow.com/questions/ask) – Bhargav Rao Oct 11 '20 at 13:21
  • @BhargavRao sorry, my bad. I'm remember this going forward. – ChickenChild77 Oct 14 '20 at 19:20

0 Answers0