为什么这个网络图在R中不能保持同一组的节点在一起?

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

Why this Network plot doesn't keep the nodes from same group together in R?

问题

我有一个名为nodes的数据框,其中包含如下组信息:

  1. dput(nodes)
  2. structure(list(id = c("AC006329.1", "AC027796.4", "AC111170.2",
  3. "AC111170.3", "AC138207.4", "AP000695.2", "CDK15", "COL14A1",
  4. "COL15A1", "DDX11-AS1", "FOXP4-AS1", "IFNG-AS1", "ITGB2-AS1",
  5. "LINC00944", "LINC01213", "LINC01395", "MIR155HG", "MSTRG.108144",
  6. "MSTRG.110466", "MSTRG.11483", "MSTRG.130624", "MSTRG.134576",
  7. "MSTRG.147129", "MSTRG.180740", "MSTRG.180762", "MSTRG.24184",
  8. "MSTRG.9061", "SERHL"), label = c("AC006329.1", "AC027796.4",
  9. "AC111170.2", "AC111170.3", "AC138207.4", "AP000695.2", "CDK15",
  10. "COL14A1", "COL15A1", "DDX11-AS1", "FOXP4-AS1", "IFNG-AS1", "ITGB2-AS1",
  11. "LINC00944", "LINC01213", "LINC01395", "MIR155HG", "MSTRG.108144",
  12. "MSTRG.110466", "MSTRG.11483", "MSTRG.130624", "MSTRG.134576",
  13. "MSTRG.147129", "MSTRG.180740", "MSTRG.180762", "MSTRG.24184",
  14. "MSTRG.9061", "SERHL"), group = structure(c(1L, 6L, 6L, 6L, 4L,
  15. 3L, 4L, 4L, 4L, 1L, 1L, 2L, 2L, 2L, 5L, 5L, 2L, 4L, 2L, 1L, 6L,
  16. 5L, 5L, 3L, 3L, 3L, 1L, 5L), levels = c("blue", "brown", "cyan",
  17. "green", "purple", "red"), class = "factor")), row.names = c(NA,
  18. -28L), class = "data.frame")

edges看起来如下:

这是edges.csv文件

我使用了以下代码来创建网络图,使用了nodesedges

当我像下面这样使用时,网络图一直在移动,根本停不下来。

  1. visNetwork(nodes, edges)

我还尝试在上面的代码中添加了visPhysics(stabilization = FALSE),但没有效果。

所以,我尝试了以下方法,使用了igraph的布局:

  1. visNetwork(nodes, edges) %>%
  2. visIgraphLayout()

这样做后,节点不会按照同一组/颜色放在一起。请问有人可以告诉我如何将同一组/颜色的节点放在一起吗?

它应该看起来像这样:

为什么这个网络图在R中不能保持同一组的节点在一起?

英文:

I have dataframe nodes with the group information like below:

  1. dput(nodes)
  2. structure(list(id = c("AC006329.1", "AC027796.4", "AC111170.2",
  3. "AC111170.3", "AC138207.4", "AP000695.2", "CDK15", "COL14A1",
  4. "COL15A1", "DDX11-AS1", "FOXP4-AS1", "IFNG-AS1", "ITGB2-AS1",
  5. "LINC00944", "LINC01213", "LINC01395", "MIR155HG", "MSTRG.108144",
  6. "MSTRG.110466", "MSTRG.11483", "MSTRG.130624", "MSTRG.134576",
  7. "MSTRG.147129", "MSTRG.180740", "MSTRG.180762", "MSTRG.24184",
  8. "MSTRG.9061", "SERHL"), label = c("AC006329.1", "AC027796.4",
  9. "AC111170.2", "AC111170.3", "AC138207.4", "AP000695.2", "CDK15",
  10. "COL14A1", "COL15A1", "DDX11-AS1", "FOXP4-AS1", "IFNG-AS1", "ITGB2-AS1",
  11. "LINC00944", "LINC01213", "LINC01395", "MIR155HG", "MSTRG.108144",
  12. "MSTRG.110466", "MSTRG.11483", "MSTRG.130624", "MSTRG.134576",
  13. "MSTRG.147129", "MSTRG.180740", "MSTRG.180762", "MSTRG.24184",
  14. "MSTRG.9061", "SERHL"), group = structure(c(1L, 6L, 6L, 6L, 4L,
  15. 3L, 4L, 4L, 4L, 1L, 1L, 2L, 2L, 2L, 5L, 5L, 2L, 4L, 2L, 1L, 6L,
  16. 5L, 5L, 3L, 3L, 3L, 1L, 5L), levels = c("blue", "brown", "cyan",
  17. "green", "purple", "red"), class = "factor")), row.names = c(NA,
  18. -28L), class = "data.frame")

And the edges look like below:

here is the edges.csv file

Using nodes and edges I used the below code to create the network plot:

When I used like below, the network plot was constantly moving and doesn't stop at all.

  1. visNetwork(nodes, edges)

I also tried adding visPhysics(stabilization = FALSE) to above line, but didn't work.

So, I tried like below with igraph layout:

  1. visNetwork(nodes, edges) %>%
  2. visIgraphLayout()

为什么这个网络图在R中不能保持同一组的节点在一起?

This doesn't keep the nodes from same group together. Can anyone please tell me how to keep the nodes from same group / color together?

It should look like below:

为什么这个网络图在R中不能保持同一组的节点在一起?

答案1

得分: 3

我通常发现使用 tidygraphggraph 生态系统来自定义网络图更容易。您的图形边数比给定的示例要多得多,因此可能需要一些调整才能使边看起来符合您的要求。

  1. library(tidygraph)
  2. library(ggraph)
  3. # 匹配给定示例中的颜色:
  4. colormap <- c(red = '#fa3233', blue = '#3d85e9',
  5. green = '#4cb30f', purple = '#7b29ef',
  6. cyan = '#e02aef', brown = '#ffa500')
  7. fillmap <- c(red = '#fb7e81', blue = '#97c2fc',
  8. green = '#7be142', purple = '#ad85e4',
  9. cyan = '#ea7cf3', brown = '#ffff00')
  10. tbl_graph(nodes,
  11. cbind(edges, colour = nodes$group[match(edges$from, nodes$id)])) %>%
  12. ggraph(layout = 'igraph', algorithm = 'fr') +
  13. geom_edge_diagonal(aes(color = colour, width = weight)) +
  14. geom_node_circle(aes(r = 1, color = group, fill = group)) +
  15. coord_equal() +
  16. scale_edge_color_manual(values = colormap) +
  17. scale_color_manual(values = colormap) +
  18. scale_fill_manual(values = fillmap) +
  19. scale_edge_width_continuous(range = c(0.1, 5)) +
  20. theme_void() +
  21. theme(legend.position = 'none')

编辑

要调整布局以使组之间不要太靠近,并以易读的方式标记节点,您可以执行以下操作:

  1. tbl_graph(nodes,
  2. cbind(edges, colour = nodes$group[match(edges$from, nodes$id)])) %>%
  3. ggraph(layout = 'igraph', algorithm = 'fr', weights = (edges$weight)^0.2) +
  4. geom_edge_diagonal(aes(color = colour, width = weight)) +
  5. geom_node_label(aes(label = label, fill = group, color = group), size = 3,
  6. label.r = unit(0.5, "lines")) +
  7. geom_node_text(aes(label = label), size = 3) +
  8. coord_equal(clip = 'off') +
  9. scale_edge_color_manual(values = colormap) +
  10. scale_color_manual(values = colormap) +
  11. scale_fill_manual(values = fillmap) +
  12. scale_edge_width_continuous(range = c(0.1, 5)) +
  13. theme_void() +
  14. theme(legend.position = 'none')

为什么这个网络图在R中不能保持同一组的节点在一起?

  1. 这是您提供的代码的翻译部分。
  2. <details>
  3. <summary>英文:</summary>
  4. I usually find it easier to customize a network graph using the `tidygraph` and `ggraph` ecosystem. Your graph has a lot more edges than the given example, so it might take some tweaks to get the edges looking as you want them.
  5. ``` r
  6. library(tidygraph)
  7. library(ggraph)
  8. # Match colours in given example:
  9. colormap &lt;- c(red = &#39;#fa3233&#39;, blue = &#39;#3d85e9&#39;,
  10. green = &#39;#4cb30f&#39;, purple = &#39;#7b29ef&#39;,
  11. cyan = &#39;#e02aef&#39;, brown = &#39;#ffa500&#39;)
  12. fillmap &lt;- c(red = &#39;#fb7e81&#39;, blue = &#39;#97c2fc&#39;,
  13. green = &#39;#7be142&#39;, purple = &#39;#ad85e4&#39;,
  14. cyan = &#39;#ea7cf3&#39;, brown = &#39;#ffff00&#39;)
  15. tbl_graph(nodes,
  16. cbind(edges, colour = nodes$group[match(edges$from, nodes$id)])) %&gt;%
  17. ggraph(layout = &#39;igraph&#39;, algorithm = &#39;fr&#39;) +
  18. geom_edge_diagonal(aes(color = colour, width = weight)) +
  19. geom_node_circle(aes(r = 1, color = group, fill = group)) +
  20. coord_equal() +
  21. scale_edge_color_manual(values = colormap) +
  22. scale_color_manual(values = colormap) +
  23. scale_fill_manual(values = fillmap) +
  24. scale_edge_width_continuous(range = c(0.1, 5)) +
  25. theme_void() +
  26. theme(legend.position = &#39;none&#39;)

为什么这个网络图在R中不能保持同一组的节点在一起?


Edit

To adjust the layout so that the groups are not too close together, and to label the nodes in a legible way, you could do:

  1. tbl_graph(nodes,
  2. cbind(edges, colour = nodes$group[match(edges$from, nodes$id)])) %&gt;%
  3. ggraph(layout = &#39;igraph&#39;, algorithm = &#39;fr&#39;, weights = (edges$weight)^0.2) +
  4. geom_edge_diagonal(aes(color = colour, width = weight)) +
  5. geom_node_label(aes(label = label, fill = group, color = group), size = 3,
  6. label.r = unit(0.5, &quot;lines&quot;)) +
  7. geom_node_text(aes(label = label), size = 3) +
  8. coord_equal(clip = &#39;off&#39;) +
  9. scale_edge_color_manual(values = colormap) +
  10. scale_color_manual(values = colormap) +
  11. scale_fill_manual(values = fillmap) +
  12. scale_edge_width_continuous(range = c(0.1, 5)) +
  13. theme_void() +
  14. theme(legend.position = &#39;none&#39;)

为什么这个网络图在R中不能保持同一组的节点在一起?

答案2

得分: 3

这是我尝试使用igraph的代码:

  1. g <- edges %>%
  2. mutate(
  3. color1 = with(nodes, as.character(group)[match(from, id)]),
  4. color2 = with(nodes, as.character(group)[match(to, id)])
  5. ) %>%
  6. mutate(color = ifelse(color1 == color2, color1, "gray")) %>%
  7. mutate(width = ifelse(color == "gray", weight, weight * 100)) %>%
  8. graph_from_data_frame(directed = FALSE) %>%
  9. set_vertex_attr(name = "color", value = with(nodes, as.character(group)[match(names(V(.)), id)])) %>%
  10. set_vertex_attr(name = "label", value = with(nodes, label[match(names(V(.)), label)])) %>%
  11. set_vertex_attr(name = "size", value = 10) %>%
  12. set_edge_attr(name = "curved", value = TRUE)

然后绘制它:

  1. plot(g, layout = layout_with_fr)

为什么这个网络图在R中不能保持同一组的节点在一起?

英文:

This is what I have tried with igraph

  1. g &lt;- edges %&gt;%
  2. mutate(
  3. color1 = with(nodes, as.character(group)[match(from, id)]),
  4. color2 = with(nodes, as.character(group)[match(to, id)])
  5. ) %&gt;%
  6. mutate(color = ifelse(color1 == color2, color1, &quot;gray&quot;)) %&gt;%
  7. mutate(width = ifelse(color == &quot;gray&quot;, weight, weight * 100)) %&gt;%
  8. graph_from_data_frame(directed = FALSE) %&gt;%
  9. set_vertex_attr(name = &quot;color&quot;, value = with(nodes, as.character(group)[match(names(V(.)), id)])) %&gt;%
  10. set_vertex_attr(name = &quot;label&quot;, value = with(nodes, label[match(names(V(.)), label)])) %&gt;%
  11. set_vertex_attr(name = &quot;size&quot;, value = 10) %&gt;%
  12. set_edge_attr(name = &quot;curved&quot;, value = TRUE)

and then plot it

  1. plot(g, layout = layout_with_fr)

为什么这个网络图在R中不能保持同一组的节点在一起?

huangapple
  • 本文由 发表于 2023年7月24日 16:15:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752553.html
匿名

发表评论

匿名网友

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

确定