简体   繁体   English

R 显示/隐藏点

[英]R show/hide points

everybody.大家。

I am using R and ggplot2 .我正在使用Rggplot2 I have a chart and there are multiple points on it (Each of them has different colors. Red, blue, yellow, etc).我有一张图表,上面有多个点(每个点都有不同的 colors。红色、蓝色、黄色等)。 I am using also R Shiny and ggiraph package to have click events on legends .我也在使用R Shinyggiraph package 在legends上有click事件。 Once, I click legend , I am rendering new chart based on filter criteria and it is fine.有一次,我单击legend ,我正在根据过滤条件渲染新图表,这很好。

Problem.问题。 When any color is removed from dataset (for example red), legend (with name red) is hided also.当从数据集中删除任何颜色(例如红色)时, legend (名称为红色)也会被隐藏。 Also, I try to hide points but this case legends are hided also.另外,我尝试隐藏点,但这种情况下的图例也被隐藏了。

What kind of solution I need this case?我需要什么样的解决方案?

Thanks !谢谢 !

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)

在此处输入图像描述

You can try with plotly :您可以尝试使用plotly

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

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM