gpt4 book ai didi

r - 可视化 iGraph 和标签对齐

转载 作者:行者123 更新时间:2023-12-05 06:32:45 27 4
gpt4 key购买 nike

代码

library(igraph)

g <- graph.tree(n = 2 ^ 3 - 1, children = 2)
node_labels <- c("", "Group A", "Group B", "(1)Text", "(2)I am a longer text", "(3)I am a long long long text", "(4)X")
lay = layout.reingold.tilford(g)

plot(g,
vertex.size = 4, # node size
vertex.color = '#C4D8E2', # node color
vertex.label = node_labels, # node labels
vertex.label.dist = 5, # node label size
vertex.label.font = 2, # node label type (bold)
vertex.label.degree=0.0,
margin=c(0,0,0,0),
layout=-lay[, 2:1]
)

给我一​​张带有标签的图表。如何对齐标签 1 .. 4,使它们左对齐,在圆的右边而不是居中对齐?从我的角度来看,第 3 条是正确的。切换到 ggraph 是一种替代方法,但这里的标签旋转不起作用。

enter image description here

最佳答案

这里是计算代码:

library(igraph)

g <- graph.tree(n = 2 ^ 3 - 1, children = 2)
node_labels <- c("", "Group A", "Group B", "(1)Text", "(2)I am a longer text", "(3)I am a long long long text", "(4)X")
lay = layout.reingold.tilford(g)

t1 <- sapply(as.vector( node_labels), FUN = strwidth,units='in')
t2 <- (t1 - min(t1))/ (max(t1) - min(t1))*3+0.9


plot(g,
vertex.size = 4, # node size
vertex.color = '#C4D8E2', # node color
vertex.label = node_labels, # node labels
vertex.label.dist = t2, # node label size
vertex.label.font = 2, # node label type (bold)
vertex.label.degree=0.0,
margin=c(0,0,0,0),
layout=-lay[, 2:1]
)

稍微好一点,但我做不到完美。

关于r - 可视化 iGraph 和标签对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51101947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com