英文:
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))
结果如下:
问题是,当我为类型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 <- 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))
The result is the following :
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 <- as.data.frame(plot_layout[["layout"]])
plot_layout <- rename(plot_layout, x = V1, y= V2)
graph <- create_layout(graph, plot_layout)
Which allowed me to use ggrepel (geom_text_repel()) to obtain a decent text annotation layout.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论