rggplot2visualizationggiraph

R show/hide points


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 !


Solution

  • 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