Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create rotated node labels for circle igraph in R
## How to rotate node labels on circle igraph in R
library(igraph)
### Example
## Generate some fake data
n <- 75
g <- erdos.renyi.game(n, 0.5)
V(g)$name = paste("long_name", 1:n, sep="_")
## laid out as a circle to begin with
la <- layout.circle(g)
par(mar=c(8,6,6,6))
plot(g, layout=la, vertex.size=2, vertex.label="")
## Apply labels manually
#Specify x and y coordinates of labels, adjust outward as desired
x = la[,1]*1.3
y = la[,2]*1.3
#create vector of angles for text based on number of nodes (flipping the orientation of the words half way around so none appear upside down)
angle = ifelse(atan(-(la[,1]/la[,2]))*(180/pi) < 0, 90 + atan(-(la[,1]/la[,2]))*(180/pi), 270 + atan(-la[,1]/la[,2])*(180/pi))
#Apply the text labels with a loop with angle as srt
for (i in 1:length(x)) {
text(x=x[i], y=y[i], labels=V(g)$name[i], adj=NULL, pos=NULL, cex=.7, col="black", srt=angle[i], xpd=T)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment