Vinod K. Ahuja
Vinod K. Ahuja

Reputation: 73

Tree graph formatting in igraph R

I have data of network data as follows:

library(igraph)

dnode<-c("a","b","c","d","e","f","g","h","i","j","k")

dedge<-data.frame("From"=c("a","b","c","d","e","f","f","f","f","f"), "To"=c("f","f","f","f","f","g","h","i","j","k"))

When I plot it in R I write code as follows:

net <- graph_from_data_frame(d=dedge, vertices=dnode, directed=T)

l<- layout_as_tree

plot(net, vertex.shape="square", layout=l, edge.arrow.mode=2, edge.arrow.width=2, edge.arrow.size=0.1)

I get the output like this:

enter image description here

I want output like this:

enter image description here

Upvotes: 1

Views: 873

Answers (1)

paqmo
paqmo

Reputation: 3729

Try using the Sugiyama layout. The syntax is a bit different from other layouts. Rather than passing a network to plot, and layout to the layout argument, you pass layout$extd_graph and no layout argument. I find the Sugiyama layout much more flexible to use with layered layouts.

l <- layout_with_sugiyama(net)
plot(l$extd_graph, 
     vertex.shape="square", 
     vertex.label=as_ids(V(net)),
     edge.arrow.mode=2, 
     edge.arrow.width=2, 
     edge.arrow.size=0.1)

enter image description here

Upvotes: 1

Related Questions