0

我有一个可视化(见下文),它具有很好的悬停功能,使可读性更容易。我希望用户能够通过单击他们悬停的东西来使悬停功能持续存在。这可以使用ggiraph吗?

最小的可重现示例代码:

library(tidyverse)
library(ggiraph)

nodes = tibble(value=c(1,2,1,2), week=c(1,1,2,2))     
vertices = tibble(From=c(1,1,2,2), To=c(1,2,1,2), weight=c(.5,.5,.5,.5), start_week=1, rand=c(.48, .33, .34,.40))
p=ggplot(nodes) +
  geom_point_interactive(size = 12, aes(x=week, y=value, color =paste(week, value), data_id=paste(week, value)))+
  geom_text_interactive(color="white",aes(x=week, y=value, label=value, data_id=paste(week, value)))+
  geom_segment_interactive(data=vertices, aes(x=start_week+.1, xend=start_week+1-.1, y=From, yend=To,
                                                                     data_id=paste(start_week, From)))+
  geom_text_interactive(data=vertices, aes(x=start_week+rand, y=From-(From-To)*rand, label=paste0(round(weight*100), "%"),
                                                                  data_id=paste(start_week, From)), vjust=-.1, hjust=-.1) +
  guides(color=F, size=F)+
  scale_y_continuous(name="Cluster", breaks=NULL, labels=NULL)+
  scale_x_continuous(name="Stage", breaks=1:6)+
  theme_minimal()

girafe(ggobj=p,
       options = list(
         opts_hover_inv(css = "opacity:0.1;"),
         opts_hover(css = "stroke-width:2;")
       ))

在此处输入图像描述

4

1 回答 1

1

您可能需要调整一些选项以使其更漂亮。这里重要的一点是定义opts_selection(only_shiny = FALSE)以便在没有闪亮的情况下也可以进行选择:

library(tidyverse)
library(ggiraph)

nodes = tibble(value=c(1,2,1,2), week=c(1,1,2,2))     
vertices = tibble(From=c(1,1,2,2), To=c(1,2,1,2), weight=c(.5,.5,.5,.5), start_week=1, rand=c(.48, .33, .34,.40))
p=ggplot(nodes) +
  geom_point_interactive(size = 12, aes(x=week, y=value, color =paste(week, value), data_id=paste(week, value)))+
  geom_text_interactive(color="white",aes(x=week, y=value, label=value, data_id=paste(week, value)))+
  geom_segment_interactive(data=vertices, aes(x=start_week+.1, xend=start_week+1-.1, y=From, yend=To,
                                              data_id=paste(start_week, From)))+
  geom_text_interactive(data=vertices, aes(x=start_week+rand, y=From-(From-To)*rand, label=paste0(round(weight*100), "%"),
                                           data_id=paste(start_week, From)), vjust=-.1, hjust=-.1) +
  guides(color=F, size=F)+
  scale_y_continuous(name="Cluster", breaks=NULL, labels=NULL)+
  scale_x_continuous(name="Stage", breaks=1:6)+
  theme_minimal()

girafe(ggobj=p,
       options = list(
         opts_hover_inv(css = "opacity:0.1;"),
         opts_hover(css = "stroke-width:2;"),
         opts_selection(only_shiny = FALSE, type = "single", css = "stroke:yellow;")
       ))

于 2021-05-12T08:45:52.570 回答