旋转顶点标签

问题描述 投票:0回答:1

使用 igraph 包绘制图形时,如何旋转图中的顶点标签,例如按 pi/4 旋转?

library(igraph)
g = graph.ring(5)
V(g)$name = paste(LETTERS[1:5], 'rotate_45')
plot.igraph(vertex.label.rotate=pi/2, g ) ##this doesn't work
r plot igraph
1个回答
0
投票

据我所知,这在 igraph 中是不可能直接实现的。但是,通过指定布局,我们可以检索顶点的坐标并添加旋转的文本。

library(igraph)
g = graph.ring(5)
V(g)$name = paste(LETTERS[1:5], 'rotate_45')

#add layout to have coordinates where we can plot the vertices labels later
coords <- layout_(g, in_circle())

plot.igraph(g, layout = coords,  vertex.label = "", rescale = F)

angles = c(45, 45, 45, 135, 77)

#Apply the text labels with angle as srt
for (i in 1:dim(coords)[1]) {
  text(x = coords[i,1], y = coords[i,2], labels = V(g)$name[i], 
       col = "black", srt = angles[i], xpd = T)
}

这会导致:

如果您想提供弧度,您可以在指定角度之前使用

rad2deg <- function(rad) {(rad * 180) / (pi)}
© www.soinside.com 2019 - 2024. All rights reserved.