Warm tip: This article is reproduced from serverfault.com, please click

ggplot2-R显示/隐藏点

(ggplot2 - R show/hide points)

发布于 2020-12-06 15:57:54

大家

我正在使用Rggplot2我有一个图表,上面有多个点(每个点都有不同的颜色。红色,蓝色,黄色等)。我也使用R Shinyggiraph打包click事件legends一次,单击legend,我将基于过滤条件渲染新图表,这很好。

问题。从数据集中删除任何颜色(例如红色)后,legend(名称为红色)也会被隐藏。另外,我尝试隐藏点,但这种情况下的图例也被隐藏。

我需要这种情况下什么样的解决方案?

谢谢 !

Questioner
varguy
Viewed
0
David Gohel 2020-12-07 00:15:57

你必须创建自己的离散比例:

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)

在此处输入图片说明