R show/hide points

1.3k Views Asked by At

everybody.

I am using R and ggplot2. I have a chart and there are multiple points on it (Each of them has different colors. Red, blue, yellow, etc). I am using also R Shiny and ggiraph package to have click events on legends. Once, I click legend, I am rendering new chart based on filter criteria and it is fine.

Problem. When any color is removed from dataset (for example red), legend (with name red) is hided also. Also, I try to hide points but this case legends are hided also.

What kind of solution I need this case ?

Thanks !

2

There are 2 best solutions below

1
On BEST ANSWER

You have to create your own discrete scale:

library(ggiraph)
library(ggplot2)

z <- ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species, tooltip = Species))+
  geom_point_interactive() + 
  scale_color_manual_interactive(values = c(setosa = "red", versicolor = "green", virginica = "yellow", yoyo = "black"),
                                 data_id = c(setosa = "setosa", versicolor = "versicolor", virginica = "virginica", yoyo = "yoyo"),
                                 tooltip = c(setosa = "setosa", versicolor = "versicolor", virginica = "virginica", yoyo = "yoyo"),
                                 limits = c("setosa", "versicolor", "virginica", "yoyo"))
girafe(ggobj = z)

enter image description here

2
On

You can try with plotly:

library(plotly)
library(ggplot2)
#Code
ggplotly(ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width,color=Species))+
  geom_point())

Output:

enter image description here