itsMeInMiami
itsMeInMiami

Reputation: 2729

Can I rotate a network graph made by igraph::graph?

I am trying to replicate a figure from a text book with the igraph package. This code draws the structure correctly:

library(igraph)
g <- graph( c( "W","X", "Y","Z", "Y","W", "Z","W"), dir=FALSE)
plot(g, vertex.shape = "none")

but the entire graphic is rotated so it does not match the book. The above plot has the X node up at 1:00 on the clock face. I would like it so it is out at 9:00. I saw a post that showed that igraph::tkplot() can be rotated but I need a static plot. Is there a way to rotate an igraph network plot that is not rendered with tkplot(g)?

Upvotes: 2

Views: 670

Answers (1)

G5W
G5W

Reputation: 37661

Yes, you just need to control the layout. The default layout is done with layout_nicely. To get that layout only rotated, just save the layout and multiply by a rotation matrix. You may need to play around a little with just how big a rotation to use.

LO = layout_nicely(g)
angle = 2*pi * 7.5/12
RotMat = matrix(c(cos(angle),sin(angle),-sin(angle), cos(angle)), ncol=2)
LO2 = LO %*% RotMat

plot(g, vertex.shape = "none", layout = LO2)

Graph with rotated layout

Upvotes: 3

Related Questions