rggplot2ggraph

arrange and color circles with ggraph package in R


I have a data frame df:

df

enter image description here

dput(df)
structure(list(group = c("root", "root", "NS2A", "NS2A", "NS2A", 
"NS5", "NS5", "NS5", "NS5", "NS5", "NS5", "NS5", "NS5", "NS5", 
"NS5", "NS5", "NS5", "NS5", "NS5", "NS5", "NS5"), subitem = c("NS2A", 
"NS5", "NS2A_C_to_A_Nonsynonymous_3931_1.0015", "NS2A_C_to_A_Nonsynonymous_3931_0.9404", 
"NS2A_C_to_A_Nonsynonymous_3931_1.0141", "NS5_G_to_A_Synonymous_8561_2.6323", 
"NS5_A_to_T_Synonymous_9728_2.4983", "NS5_G_to_A_Synonymous_8561_2.5331", 
"NS5_A_to_T_Synonymous_9728_2.3962", "NS5_G_to_A_Synonymous_8561_2.1411", 
"NS5_A_to_T_Synonymous_9728_2.6646", "NS5_G_to_A_Synonymous_8561_2.0874", 
"NS5_A_to_T_Synonymous_9728_2.6065", "NS5_G_to_A_Synonymous_8561_2.3789", 
"NS5_A_to_T_Synonymous_9728_2.6322", "NS5_G_to_A_Synonymous_8561_3.2712", 
"NS5_A_to_C_Nonsynonymous_9276_2.8576", "NS5_A_to_T_Synonymous_9728_2.7555", 
"NS5_G_to_A_Synonymous_8561_1.9121", "NS5_A_to_C_Nonsynonymous_9276_5.1328", 
"NS5_A_to_T_Synonymous_9728_2.6368"), size = c(0, 0, 1.0015, 
0.9404, 1.0141, 2.6323, 2.4983, 2.5331, 2.3962, 2.1411, 2.6646, 
2.0874, 2.6065, 2.3789, 2.6322, 3.2712, 2.8576, 2.7555, 1.9121, 
5.1328, 2.6368), Pass = c("PA", "PA", "P1", "P7", "P12", "P0", 
"P0", "P1", "P1", "P3", "P3", "P5", "P5", "P7", "P7", "P10", 
"P10", "P10", "P12", "P12", "P12"), cc = c("#FFFFFF", "#FFFFFF", 
"#fc2f92", "#b84592", "#8e43e7", "#3369e7", "#3369e7", "#fc2f92", 
"#fc2f92", "#00aeff", "#00aeff", "#ff4f81", "#ff4f81", "#b84592", 
"#b84592", "#Cf6c5f", "#Cf6c5f", "#Cf6c5f", "#8e43e7", "#8e43e7", 
"#8e43e7")), row.names = c(NA, -21L), class = "data.frame")

I want to create a root white circle which contains two white sub circles each of them containing those subitems listed. Colors are assigned in df$cc

vertices <- df %>% 
  distinct(subitem, size) %>% 
  add_row(subitem = "root", size = 0)

graph <- graph_from_data_frame(df, vertices = vertices)


dffff <- data.frame(group="root",    
                    subitem="root", 
                    size=0,
                    Pass="PA",
                    cc="#FFFFFF")
cc <- c("#FFFFFF","#fc2f92","#b84592","#8e43e7","#3369e7","#00aeff","#ff4f81","#Cf6c5f")

df2=data.frame(Pass=unique(df$Pass),cc=cc)
data2<-dplyr::inner_join(df,df2)
df<-data2
ggg<-rbind(df,dffff)
ggg <- mutate_at(ggg, vars(cc), as.factor)


ggraph(graph, layout = "circlepack", weight = size) + 
  geom_node_circle(aes(fill=ggg$cc,colour = size,group=size)) +
  coord_fixed()

However, the plot I get, it has the wrong color assigned to each circle and the largest subitem in NS5 subcircle gets arranged in the middle (here it is the large blu circle) but I do not want in the middle

enter image description here


Solution

  • Getting the right colors could be easily fixed. First, to get the right color assignments add the colors to the vertices of your graph object using igraph::V(). Second, as your colors are color codes use scale_fill_identity. Unfortunately, after a look at the docs I think there are only limited options to customize the positions of the nodes in a network.

    library(igraph)
    library(ggraph)
    
    set.seed(123)
    
    V(graph)$color <- ggg$cc
    
    ggraph(graph,
      layout = "circlepack",
      weight = size
    ) +
      geom_node_circle(aes(fill = color, colour = size, group = size)) +
      scale_fill_identity() +
      coord_fixed()