1

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 !

varguy
  • 15
  • 4

2 Answers2

2

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

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • no problem. If you are new to ggplot2, you should read that part: https://ggplot2.tidyverse.org/reference/scale_manual.html – David Gohel Dec 06 '20 at 16:25
0

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

Duck
  • 39,058
  • 13
  • 42
  • 84
  • Thanks for reply. I tried `plotly` project, but my ggplot object has background_image. It seems, that `plotly` doesn't respect background_images. Also, I posted question about it yesterday. – varguy Dec 06 '20 at 16:14
  • https://stackoverflow.com/questions/65156155/r-renderplotly-has-white-background – varguy Dec 06 '20 at 16:14