在一个数据框中使用 ggraph 创建多个图。

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

Multiple plots in ggraph using one dataframe

问题

以下是代码的中文翻译:

  1. # 这是我使用的数据,同时我附上了下面的示例图。
  2. df <- data.frame(
  3. "PersonName1" = c("F","H","H","H","K","K","K","K","N","N","G","N","N","N","K","G","K"),
  4. "PersonName2" = c("G","G", "F","N","N","F","H","G","G","F","H","F","H","F","F","F","H"),
  5. "P" = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0, 0,0.5,0,0.5,0.5,0.5,0.5),
  6. "Country" = c("UK","UK","UK","UK","UK","UK","UK","UK","UK","UK","UK","Rwanda","Rwanda","Vietnam","Vietnam","Vietnam","Vietnam"))
  7. # 这是我使用的代码,它只生成一个图,并且我还使用了自定义的图例。我想要得到这样的图。在ggraph中是否有函数可以实现这个?
  8. install.packages('ragg')
  9. install.packages('ggraph')
  10. library(ggraph)
  11. library(tidygraph)
  12. install.packages("cowplot")
  13. library(cowplot)
  14. library(gridExtra)
  15. df <- data.frame(
  16. "PersonName1" = c("No Speaker","No Speaker"),
  17. "PersonName2" = c("Fauci","Hanks"),
  18. "P" = c(0.5, 0) )
  19. df %>%
  20. as_tbl_graph() %>%
  21. activate(edges) %>%
  22. tidygraph::filter(P > 0) %>%
  23. activate(nodes) %>%
  24. ggraph(layout = 'circle') +
  25. geom_node_text(aes(label = name), size = 8,
  26. nudge_x = c(0.2, 0, -0.2, -0.2, 0),
  27. nudge_y = c(0.3, 0.3, 0.3, -0.3, -0.3),
  28. check_overlap = TRUE) +
  29. geom_edge_link2(aes(width = after_stat(index)), color = "red",
  30. alpha = 0.5) +
  31. geom_node_point(size = 20) +
  32. scale_edge_width(range = c(0, 15), guide = 'none') +
  33. coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  34. theme_void() -> p_gp
  35. # 图例
  36. data.frame(lg1 = "Y", lg2 = "X" , P = 0.5) %>%
  37. as_tbl_graph() %>%
  38. activate(edges) %>%
  39. tidygraph::filter(P > 0) %>%
  40. activate(nodes) %>%
  41. ggraph(layout = 'circle') +
  42. geom_node_text(aes(label = name), size = 4,
  43. nudge_y = c(-0.15, -0.15, -0.15, -0.15, -0.15)) +
  44. geom_edge_link2(aes(width = after_stat(index)), color = "grey",
  45. alpha = 0.5) +
  46. geom_node_point(size = 10) +
  47. scale_edge_width(range = c(0, 5), guide = 'none') +
  48. coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  49. theme_void() -> p_lg
  50. ggdraw(p_lg) +
  51. draw_label("Significance", y = 0.66, size = 14) +
  52. draw_label("X significantly", y = 0.62, size = 10) +
  53. draw_label("exceeds Y", y = 0.58, size = 10) +
  54. draw_label("(p < 0.05)", y = 0.54, size = 10) -> p_lg
  55. # 网格
  56. grid.arrange(p_gp, p_lg,
  57. ncol=2, nrow=1, widths=c(9/10,1/10))

请注意,这是您提供的代码的中文翻译,不包括代码的执行结果或任何其他信息。

英文:

This is the data I'm using also I'm attaching the sample figure down below.

  1. df &lt;- data.frame(
  2. &quot;PersonName1&quot; = c(&quot;F&quot;,&quot;H&quot;,&quot;H&quot;,&quot;H&quot;,&quot;K&quot;,&quot;K&quot;,&quot;K&quot;,&quot;K&quot;,&quot;N&quot;,&quot;N&quot;,&quot;G&quot;,&quot;N&quot;,&quot;N&quot;,&quot;N&quot;,&quot;K&quot;,&quot;G&quot;,&quot;K&quot;),
  3. &quot;PersonName2&quot; = c(&quot;G&quot;,&quot;G&quot;, &quot;F&quot;,&quot;N&quot;,&quot;N&quot;,&quot;F&quot;,&quot;H&quot;,&quot;G&quot;,&quot;G&quot;,&quot;F&quot;,&quot;H&quot;,&quot;F&quot;,&quot;H&quot;,&quot;F&quot;,&quot;F&quot;,&quot;F&quot;,&quot;H&quot;),
  4. &quot;P&quot; = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0, 0,0.5,0,0.5,0.5,0.5,0.5),
  5. &quot;Country&quot; = c(&quot;UK&quot;,&quot;UK&quot;,&quot;UK&quot;,&quot;UK&quot;,&quot;UK&quot;,&quot;UK&quot;,&quot;UK&quot;,&quot;UK&quot;,&quot;UK&quot;,&quot;UK&quot;,&quot;UK&quot;,&quot;Rwanda&quot;,&quot;Rwanda&quot;,&quot;Vietnam&quot;,&quot;Vietnam&quot;,&quot;Vietnam&quot;,&quot;Vietnam&quot;))

在一个数据框中使用 ggraph 创建多个图。

This is the code I'm using it generates only one figure also I'm using a customised legend as well. I want to get a plot like this. Is there any function in ggraph inorder to implement this?

  1. install.packages(&#39;ragg&#39;)
  2. install.packages(&#39;ggraph&#39;)
  3. library(ggraph)
  4. library(tidygraph)
  5. install.packages(&quot;cowplot&quot;)
  6. library(cowplot)
  7. library(gridExtra)
  8. df &lt;- data.frame(
  9. &quot;PersonName1&quot; = c(&quot;No Speaker&quot;,&quot;No Speaker&quot;),
  10. &quot;PersonName2&quot; = c(&quot;Fauci&quot;,&quot;Hanks&quot;),
  11. &quot;P&quot; = c(0.5, 0) )
  12. df %&gt;%
  13. as_tbl_graph() %&gt;%
  14. activate(edges) %&gt;%
  15. tidygraph::filter(P &gt; 0) %&gt;%
  16. activate(nodes) %&gt;%
  17. ggraph(layout = &#39;circle&#39;) +
  18. geom_node_text(aes(label = name), size = 8,
  19. nudge_x = c(0.2, 0, -0.2, -0.2, 0),
  20. nudge_y = c(0.3, 0.3, 0.3, -0.3, -0.3),
  21. check_overlap = TRUE) +
  22. geom_edge_link2(aes(width = after_stat(index)), color = &quot;red&quot;,
  23. alpha = 0.5) +
  24. geom_node_point(size = 20) +
  25. scale_edge_width(range = c(0, 15), guide = &#39;none&#39;) +
  26. coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  27. theme_void() -&gt;p_gp
  28. ### legend
  29. data.frame(lg1 = &quot;Y&quot;, lg2 = &quot;X&quot; , P = 0.5) %&gt;%
  30. as_tbl_graph() %&gt;%
  31. activate(edges) %&gt;%
  32. tidygraph::filter(P &gt; 0) %&gt;%
  33. activate(nodes) %&gt;%
  34. ggraph(layout = &#39;circle&#39;) +
  35. geom_node_text(aes(label = name), size = 4,
  36. nudge_y = c(-0.15, -0.15, -0.15, -0.15, -0.15)) +
  37. geom_edge_link2(aes(width = after_stat(index)), color = &quot;grey&quot;,
  38. alpha = 0.5) +
  39. geom_node_point(size = 10) +
  40. scale_edge_width(range = c(0, 5), guide = &#39;none&#39;) +
  41. coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  42. theme_void() -&gt; p_lg
  43. ggdraw(p_lg) +
  44. draw_label(&quot;Significance&quot;, y = 0.66, size = 14) +
  45. draw_label(&quot;X significantly&quot;, y = 0.62, size = 10) +
  46. draw_label(&quot;exceeds Y&quot;, y = 0.58, size = 10) +
  47. draw_label(&quot;(p &lt; 0.05)&quot;, y = 0.54, size = 10) -&gt; p_lg
  48. ### grid
  49. grid.arrange(p_gp, p_lg,
  50. ncol=2, nrow=1, widths=c(9/10,1/10))

答案1

得分: 1

  1. library(ggraph)
  2. library(tidygraph)
  3. library(patchwork)
  4. library(cowplot)

让我们创建一些数据,可以生成你示例中的图形。边缘数据包含一个类型列,将每条边与三个国家之一关联起来。

  1. node_names <-
  2. c("Hanks", "Kardashian", "Fauci", "Government", "No Speaker")
  3. types <- c("Brazil", "South Korea", "Italy")
  4. n <- 500
  5. df <-
  6. data.frame(
  7. from = sample(node_names, n, replace = TRUE),
  8. to = sample(node_names, n, replace = TRUE),
  9. P = abs(rnorm(n)),
  10. type = sample(types, n, replace = TRUE)
  11. ) %>%
  12. distinct(from, to, .keep_all = TRUE)

现在我们使用 ggraph::facet_edges() 创建三个单独的图。

  1. p_gp <-
  2. df %>%
  3. as_tbl_graph() %>%
  4. activate(edges) %>%
  5. tidygraph::filter(P > 0) %>%
  6. activate(nodes) %>%
  7. ggraph(layout = 'circle') +
  8. geom_node_text(
  9. aes(label = name),
  10. nudge_x = c(0.2, 0,-0.2,-0.2, 0),
  11. nudge_y = c(0.3, 0.3, 0.3,-0.3,-0.3),
  12. check_overlap = TRUE
  13. ) +
  14. geom_edge_link2(aes(width = after_stat(index), color = type), ,
  15. alpha = 0.5) +
  16. geom_node_point() +
  17. scale_edge_width(range = c(0, 2), guide = 'none') +
  18. scale_edge_color_manual(values = c("green", "blue", "red"), guide = "none") +
  19. coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  20. facet_edges( ~ type) +
  21. theme_void() +
  22. theme(aspect.ratio = 1) # 确保分面始终为正方形。
  23. ### 图例
  24. p_lg <- ...

要添加自定义图例,我们可以使用 patchwork 包。

  1. p_gp / p_lg +
  2. plot_layout(heights = c(5, 1))

在一个数据框中使用 ggraph 创建多个图。

英文:
  1. library(ggraph)
  2. library(tidygraph)
  3. library(patchwork)
  4. library(cowplot)

Let’s create some data that can create the graphs in your example. The
edge data contains a column type that associates each edge with
one of the three countries.

  1. node_names &lt;-
  2. c(&quot;Hanks&quot;, &quot;Kardashian&quot;, &quot;Fauci&quot;, &quot;Government&quot;, &quot;No Speaker&quot;)
  3. types &lt;- c(&quot;Brazil&quot;, &quot;South Korea&quot;, &quot;Italy&quot;)
  4. n &lt;- 500
  5. df &lt;-
  6. data.frame(
  7. from = sample(node_names, n, replace = TRUE),
  8. to = sample(node_names, n, replace = TRUE),
  9. P = abs(rnorm(n)),
  10. type = sample(types, n, replace = TRUE)
  11. ) |&gt;
  12. distinct(from, to, .keep_all = TRUE)

Now we use ggraph::facet_edges() to create three separate plots/facets.

  1. p_gp &lt;-
  2. df %&gt;%
  3. as_tbl_graph() %&gt;%
  4. activate(edges) %&gt;%
  5. tidygraph::filter(P &gt; 0) %&gt;%
  6. activate(nodes) %&gt;%
  7. ggraph(layout = &#39;circle&#39;) +
  8. geom_node_text(
  9. aes(label = name),
  10. nudge_x = c(0.2, 0,-0.2,-0.2, 0),
  11. nudge_y = c(0.3, 0.3, 0.3,-0.3,-0.3),
  12. check_overlap = TRUE
  13. ) +
  14. geom_edge_link2(aes(width = after_stat(index), color = type), ,
  15. alpha = 0.5) +
  16. geom_node_point() +
  17. scale_edge_width(range = c(0, 2), guide = &#39;none&#39;) +
  18. scale_edge_color_manual(values = c(&quot;green&quot;, &quot;blue&quot;, &quot;red&quot;), guide = &quot;none&quot;) +
  19. coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  20. facet_edges( ~ type) +
  21. theme_void() +
  22. theme(aspect.ratio = 1) # this makes sure that the facets are always square.
  23. ### legend
  24. p_lg &lt;-
  25. data.frame(lg1 = &quot;Y&quot;, lg2 = &quot;X&quot; , P = 0.5) %&gt;%
  26. as_tbl_graph() %&gt;%
  27. activate(edges) %&gt;%
  28. tidygraph::filter(P &gt; 0) %&gt;%
  29. activate(nodes) %&gt;%
  30. ggraph(layout = &#39;circle&#39;) +
  31. geom_node_text(
  32. aes(label = name),
  33. size = 4,
  34. nudge_y = c(-0.15,-0.15,-0.15,-0.15,-0.15)
  35. ) +
  36. geom_edge_link2(aes(width = after_stat(index)),
  37. color = &quot;grey&quot;,
  38. alpha = 0.5) +
  39. geom_node_point(size = 10) +
  40. scale_edge_width(range = c(0, 5), guide = &#39;none&#39;) +
  41. coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  42. theme_void()
  43. p_lg &lt;-
  44. ggdraw(p_lg) +
  45. draw_label(&quot;Significance&quot;, y = 1.2, size = 14) +
  46. draw_label(&quot;X significantly&quot;, y = 1, size = 10) +
  47. draw_label(&quot;exceeds Y&quot;, y = .8, size = 10) +
  48. draw_label(&quot;(p &lt; 0.05)&quot;, y = .6, size = 10)
  49. #&gt; Warning in y + params$y: longer object length is not a multiple of shorter
  50. #&gt; object length
  51. #&gt; Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
  52. #&gt; ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.
  53. #&gt; This warning is displayed once every 8 hours.
  54. #&gt; Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
  55. #&gt; generated.

To add in the custom legend we can use the patchwork package.

  1. p_gp / p_lg +
  2. plot_layout(heights = c(5, 1))

在一个数据框中使用 ggraph 创建多个图。<!-- -->

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

发表评论

匿名网友

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

确定