Home > database >  How do I mark the center node in a tidygraph
How do I mark the center node in a tidygraph

Time:03-29

Using this reproducable example from another question. How do I label / colour the center node on which the local neighborhood graph is based. (In this case '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()

What I get:

enter image description here

What I want:

enter image description here

I have the feeling there should be some kind of conditional fill for geom_node_point but I don't know if this is possible...

CodePudding user response:

You can do:

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()

enter image description here

  • Related