如何在 tidygraph 中标记中心节点

How do I mark the center node in a tidygraph

使用另一个问题中的这个可重现的例子。如何标记/着色局部邻域图所基于的中心节点。 (在这种情况下 'x')

library(tidygraph)
library(ggraph)


# Example
net <- tibble::tibble(A = letters[1:6],
                      B = rep(c("x", "y"), each = 3)) %>% 
  tidygraph::as_tbl_graph()

net %>%
  tidygraph::convert(to_local_neighborhood,
                     node = which(.N()$name == "x"),
                     order = 1,
                     mode = "all") %>%
  ggraph(layout = "nicely") +
  geom_edge_link() +
  geom_node_point(size = 10, fill = "white", shape = 21) +
  geom_node_text(aes(label = name)) +
  theme_graph()

我得到的:

我想要的:

我觉得 geom_node_point 应该有某种条件填充,但我不知道这是否可能...

你可以这样做:

net %>%
  tidygraph::convert(to_local_neighborhood,
                     node = which(.N()$name == 'x'),
                     order = 1,
                     mode = "all") %>%
  mutate(root = ifelse(node_is_center(), 'red', 'white')) %>%
  ggraph(layout = "nicely") +
  geom_edge_link() +
  geom_node_point(size = 10, aes(fill = root), shape = 21) +
  geom_node_text(aes(label = name)) +
  scale_fill_identity() +
  theme_graph()