“Partial modification of layout in igraph” 的中文翻译是 “igraph 中布局的部分修改”。

huangapple go评论71阅读模式
英文:

Partial modification of layout in igraph

问题

我目前正在尝试修改一个igraph对象。我使用以下代码创建了布局:

graph <- igraph::graph_from_data_frame(d=edge, vertices = nodes, directed = FALSE)

V(graph)$color <- ifelse(nodes[V(graph), 2] == "A", "red",
                         ifelse(nodes[V(graph), 2] == "B", "blue", "white"))
V(graph)$layer <- ifelse(V(graph)$TYPE == "B", 2, ifelse(V(graph)$TYPE == "C", 1, 3))

plot_layout <- layout_with_sugiyama(graph, layers = V(graph)$layer)

plot(graph, layout = plot_layout, vertex.size = 5,
      vertex.label = NA,
      vertex.label.dist = 0, vertex.label.cex = 0.6,
      vertex.label.color = "black",
      edge.curved = 0.2, edge.width = 1, edge.color=adjustcolor("grey", 0.3))

结果如下:
“Partial modification of layout in igraph” 的中文翻译是 “igraph 中布局的部分修改”。

问题是,当我为类型A和C的节点添加标签时,它们不可读,因为节点太靠近了。您是否知道如何将仅属于层1和3的节点转换为弧而不是线,或者将节点间距开?

我尝试使用ggraph并使用ggrepel来避免标签重叠,但无法在ggraph中使用3层Sugiyama。

谢谢您的帮助。

英文:

I am currently trying to modify an igraph object. I used the following code to create the layout :

graph &lt;- igraph::graph_from_data_frame(d=edge, vertices = nodes, directed = FALSE)

V(graph)$color &lt;- ifelse(nodes[V(graph), 2] == &quot;A&quot;, &quot;red&quot;,
                         ifelse(nodes[V(graph), 2] == &quot;B&quot;, &quot;blue&quot;, &quot;white&quot;))
V(graph)$layer &lt;- ifelse(V(graph)$TYPE == &quot;B&quot;,2, ifelse(V(graph)$TYPE == &quot;C&quot;, 1,3))

plot_layout &lt;- layout_with_sugiyama(graph, layers = V(graph)$layer)

plot(graph, layout = plot_layout, vertex.size = 5,
      vertex.label = NA,
     vertex.label.dist = 0, vertex.label.cex = 0.6,
     vertex.label.color = &quot;black&quot;,
     edge.curved = 0.2, edge.width = 1, edge.color=adjustcolor(&quot;grey&quot;, 0.3))

The result is the following :
“Partial modification of layout in igraph” 的中文翻译是 “igraph 中布局的部分修改”。

The issue is that, when i add the label for nodes with type A and C they are not readable because the nodes are too close to each other. Do you know a way to transform only the nodes in layer 1 and 3 into arcs instead of line or space the node ?

I tried using ggraph to use ggrepel to avoid label overlap but could not use the 3 layers Sugiyama in ggraph.

Thank you for your help.

答案1

得分: 0

可以使用在 igraph 创建的布局,通过以下代码在 ggraph 中使用:

# 添加布局并尝试使用 ggraph 和 ggrepel 避免重叠
plot_layout <- as.data.frame(plot_layout[["layout"]]) 
plot_layout <- rename(plot_layout, x = V1, y= V2)
graph <- create_layout(graph, plot_layout) 

这使我能够使用 ggrepel(geom_text_repel())获得一个体面的文本注释布局。

英文:

It was possible to use the layout created in igraph to ggraph using the following code :

#adding the layout and trying to use ggraph and ggrepel to avoid overlap
plot_layout &lt;- as.data.frame(plot_layout[[&quot;layout&quot;]]) 
plot_layout &lt;- rename(plot_layout, x = V1, y= V2)
graph &lt;- create_layout(graph, plot_layout) 

Which allowed me to use ggrepel (geom_text_repel()) to obtain a decent text annotation layout.

huangapple
  • 本文由 发表于 2023年6月19日 21:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定