如何在 ggraph 中隐藏未连接的节点
How to hide unconnected nodes in ggraph
给定以下示例代码,
library(tidyverse)
library(tidygraph)
library(ggraph)
reprex <- tibble(to = 1:10,
from = c(2:10, 1),
facet = rep(1:2, each = 5)) %>%
as_tbl_graph()
reprex_plot <- reprex %>%
ggraph() +
geom_node_point() +
geom_edge_link()
reprex_plot + facet_edges(~ facet)
如何隐藏没有边进入或离开节点的节点?[=11=]
library(tidyverse)
library(tidygraph)
library(ggraph)
reprex2 <- tibble(to = 1:10,
from = c(2:10, 1)) %>%
as_tbl_graph() %>%
activate(nodes) %>%
mutate(facet = rep(1:2, each = 5))
reprex_plot <- reprex2 %>%
ggraph() +
geom_node_point() +
geom_edge_link() +
geom_node_label(aes(label = name)) +
theme_graph() +
facet_nodes(~ facet)
reprex_plot
我可以理解你的方法,但是 tidygraph
的 as_tbl_graph()
的智慧会带来困难。您实际上是在向它传递一个边列表,其中 facet 是一个仅适用于边的变量。您可以通过执行 reprex %>% activate(nodes) %>% as_tibble()
来验证这一点,以查看构面列与节点没有关联。
我的解决方案是在节点上显式构建 facet 列,然后使用 facet_nodes()
,这与 facet_edges()
相反,因为
Edges are drawn if their terminal nodes are both present in a panel.
给定以下示例代码,
library(tidyverse)
library(tidygraph)
library(ggraph)
reprex <- tibble(to = 1:10,
from = c(2:10, 1),
facet = rep(1:2, each = 5)) %>%
as_tbl_graph()
reprex_plot <- reprex %>%
ggraph() +
geom_node_point() +
geom_edge_link()
reprex_plot + facet_edges(~ facet)
如何隐藏没有边进入或离开节点的节点?[=11=]
library(tidyverse)
library(tidygraph)
library(ggraph)
reprex2 <- tibble(to = 1:10,
from = c(2:10, 1)) %>%
as_tbl_graph() %>%
activate(nodes) %>%
mutate(facet = rep(1:2, each = 5))
reprex_plot <- reprex2 %>%
ggraph() +
geom_node_point() +
geom_edge_link() +
geom_node_label(aes(label = name)) +
theme_graph() +
facet_nodes(~ facet)
reprex_plot
我可以理解你的方法,但是 tidygraph
的 as_tbl_graph()
的智慧会带来困难。您实际上是在向它传递一个边列表,其中 facet 是一个仅适用于边的变量。您可以通过执行 reprex %>% activate(nodes) %>% as_tibble()
来验证这一点,以查看构面列与节点没有关联。
我的解决方案是在节点上显式构建 facet 列,然后使用 facet_nodes()
,这与 facet_edges()
相反,因为
Edges are drawn if their terminal nodes are both present in a panel.