R: adjusting legend labels to the selected (gg)plotly output

251 Views Asked by At

I am using ggplotly object to visualize a scatterplot in a shiny dashboard. I have a plot colored using the values of a column. However, when I want to look at a certain part of the plot, I zoom in to know more about the points. Then the legend labels should adjust according to the points present in the visible window or selected/chosen region.

For example, I have a scatterplot below with full data and the cut column has five different values.

library(plotly)

set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]

p <- ggplot(data = d, aes(x = carat, y = price, color = cut)) + geom_point()

ggplotly(p)

The output:

enter image description here

When I select a window of the plot, there are no points related to Fair or Good in the above plot. How to avoid them in the legend labels? How to adjust/update the labels to the selected region(example, as shown below)? Should this handled using shiny reactive features?

enter image description here

0

There are 0 best solutions below