Home > Enterprise >  how to make a distinct graph plot based on one variable
how to make a distinct graph plot based on one variable

Time:12-08

I have a data like this

df<- structure(list(14, FALSE, c(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 
13, 6), c(0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 0), c(0, 1, 2, 
3, 4, 12, 5, 6, 7, 8, 9, 10, 11), c(0, 1, 2, 3, 4, 12, 5, 6, 
7, 8, 9, 10, 11), c(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13), c(0, 6, 6, 6, 6, 6, 6, 13, 13, 13, 13, 13, 13, 13, 13
), list(c(1, 0, 1), structure(list(), names = character(0)), 
    list(name = c("Bestman", "Tera1", "Tera2", "Tera3", "Tera4", 
    "Tera5", "Tetra", "Brownie1", "Brownie2", "Brownie3", "Brownie4", 
    "Brownie5", "Brownie6", "Brownie7")), list()), <environment>), class = "igraph")

I can plot it like this

plot(df) 

if I want to remove the label , I can do this

plot(df,vertex.label=NA)

but it removes it for all and I want to keep the core

I want to be able to plot it with ggplot , removing the label on each node but leave the main core label on , remove the line around the circles

CodePudding user response:

Your dput is not directly reproducible (igraphs contain an environment which isn't included in a dput). The graph I can recover has two linked central nodes, each with several child nodes.

We can draw graphs with plentiful customization options using the tidygraph and ggraph packages:

library(ggraph)
library(tidygraph)
library(extrafont)

as_tbl_graph(g) %>%
  activate(nodes) %>%
  mutate(type = ifelse(name %in% c("Bestman", "Tetra"), "root", "branch")) %>%
  mutate(group = ifelse(name == "Bestman" | grepl("Tera", name),
                      "Bestman", "Tera")) %>%
  ggraph(layout = "igraph", algorithm = "nicely")  
  geom_edge_link(width = 2, alpha = 0.1)  
  geom_node_circle(aes(r = ifelse(type == "root", 0.4, 0.1), fill = group),
                   color = NA)  
  geom_node_text(aes(label = ifelse(type == "root", name, "")), size = 5,
                 color = "gray40", family = "Roboto Condensed", fontface = 2)  
  theme_graph()  
  coord_equal()  
  scale_fill_brewer(palette = "Pastel2", guide = "none")

enter image description here

  • Related