Search code examples
rggplot2igraphggraph

Partial modification of layout in igraph


I am currently trying to modify an igraph object. I used the following code to create the layout :

graph <- igraph::graph_from_data_frame(d=edge, vertices = nodes, directed = FALSE)

V(graph)$color <- ifelse(nodes[V(graph), 2] == "A", "red",
                         ifelse(nodes[V(graph), 2] == "B", "blue", "white"))
V(graph)$layer <- ifelse(V(graph)$TYPE == "B",2, ifelse(V(graph)$TYPE == "C", 1,3))

plot_layout <- layout_with_sugiyama(graph, layers = V(graph)$layer)

plot(graph, layout = plot_layout, vertex.size = 5,
      vertex.label = NA,
     vertex.label.dist = 0, vertex.label.cex = 0.6,
     vertex.label.color = "black",
     edge.curved = 0.2, edge.width = 1, edge.color=adjustcolor("grey", 0.3))

The result is the following : Plot Result

The issue is that, when i add the label for nodes with type A and C they are not readable because the nodes are too close to each other. Do you know a way to transform only the nodes in layer 1 and 3 into arcs instead of line or space the node ?

I tried using ggraph to use ggrepel to avoid label overlap but could not use the 3 layers Sugiyama in ggraph.

Thank you for your help.


Solution

  • It was possible to use the layout created in igraph to ggraph using the following code :

    #adding the layout and trying to use ggraph and ggrepel to avoid overlap
    plot_layout <- as.data.frame(plot_layout[["layout"]]) 
    plot_layout <- rename(plot_layout, x = V1, y= V2)
    graph <- create_layout(graph, plot_layout) 
    

    Which allowed me to use ggrepel (geom_text_repel()) to obtain a decent text annotation layout.