pengchy
pengchy

Reputation: 820

how to sort the vertex according to degree in igraph layout_in_circle

When I visualize the network using igraph:

dt1 <- data.frame(v1=sample(letters[1:10],20,replace=TRUE),
                  v2=sample(letters[1:10],20,replace=TRUE))
g<-graph.data.frame(dt1, directed=F)
plot(g,layout=layout_in_circle)

enter image description here

I want to plot the vertex in the order with degree decreasing. How can I do that? I have using degree(g) to get the degree information. However, the order of the nodes plotted is according to the inner order in the g object, i.e. the index number. If you assign the name of the nodes using V(g)$name <-, the name changed, but the ID to the name relation also changed.

Upvotes: 3

Views: 3787

Answers (1)

Tam&#225;s
Tam&#225;s

Reputation: 48051

Please read the documentation of layout_in_circle (i.e. type help(layout_in_circle) in R); it says that the function has an argument named order:

order: The vertices to place on the circle, in the order of their desired placement. Vertices that are not included here will be placed at (0,0)

So, all you need to do is to create an order vector based on the degrees of the vertices using the order() function in R and then pass that to the order argument to create the layout:

> layout <- layout_in_circle(g, order=order(degree(g)))
> plot(g, layout=layout)

Upvotes: 8

Related Questions