0
votes

A feature of plotly that I really like is the ability to dive into the data by clicking on specific groupings in the legend. For example, if I set a column to color for a scatter plot, I can filter on the various color variables. However, I only know how to create this filter when assigning the column to color. Is there a way to assign a variable to the legend to filter without changing the design of the plot. For example is there a function like legend_filter in plotly I could use:

iris2 <- iris  
iris2$sample <- sample(c('A','B'), nrow(iris2), replace = T)

p <- plot_ly(data = iris2, x = ~Sepal.Length, y = ~Petal.Length, color = ~Species,
#                   legend_filter = ~sample
                     )
p

such that 'A' and 'B' show up in the side bar to interactively click on, but aren't referenced on the graph?

Thanks

1
this might help: community.plot.ly/t/…MLavoie

1 Answers

1
votes

This method lets you toggle all of A and B on off as a group by clicking any one of the entries.

Legend is definitely cluttered side, and you have to add another set of markers for each level of your grouping variable. I don't think this is really the end result you're looking for, but I figured I might as well post anyway in case any pieces of it are useful.

plot_ly() %>% 
  add_markers(data = iris2[iris2$sample == "A",],
              x = ~Sepal.Length,
              y = ~Petal.Length,
              color = ~Species,
              legendgroup = "A",
              name = "A") %>% 
  add_markers(data = iris2[iris2$sample == "B",],
              x = ~Sepal.Length,
              y = ~Petal.Length,
              color = ~Species,
              legendgroup = "B",
              name = "B")

Yields plot