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

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

Multiple plots in ggraph using one dataframe

问题

以下是代码的中文翻译:

# 这是我使用的数据,同时我附上了下面的示例图。
df <- data.frame( 
  "PersonName1" = c("F","H","H","H","K","K","K","K","N","N","G","N","N","N","K","G","K"), 
  "PersonName2" = c("G","G", "F","N","N","F","H","G","G","F","H","F","H","F","F","F","H"), 
  "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), 
  "Country" = c("UK","UK","UK","UK","UK","UK","UK","UK","UK","UK","UK","Rwanda","Rwanda","Vietnam","Vietnam","Vietnam","Vietnam")) 

# 这是我使用的代码,它只生成一个图,并且我还使用了自定义的图例。我想要得到这样的图。在ggraph中是否有函数可以实现这个?
install.packages('ragg')
install.packages('ggraph')
library(ggraph)
library(tidygraph)
install.packages("cowplot")
library(cowplot)
library(gridExtra)

df <- data.frame( 
  "PersonName1" = c("No Speaker","No Speaker"), 
  "PersonName2" = c("Fauci","Hanks"), 
  "P" = c(0.5, 0) )

df %>%
  as_tbl_graph() %>%
  activate(edges) %>%
  tidygraph::filter(P > 0) %>%
  activate(nodes) %>%
  ggraph(layout = 'circle') +
  geom_node_text(aes(label = name), size = 8, 
                 nudge_x = c(0.2, 0, -0.2, -0.2, 0), 
                 nudge_y = c(0.3, 0.3, 0.3, -0.3, -0.3),
                 check_overlap = TRUE) + 
  geom_edge_link2(aes(width = after_stat(index)), color = "red",
                  alpha = 0.5) +
  geom_node_point(size = 20)  +
  scale_edge_width(range = c(0, 15), guide = 'none') +
  coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  theme_void() -> p_gp

# 图例
data.frame(lg1 = "Y", lg2 = "X" , P = 0.5) %>% 
  as_tbl_graph() %>%
  activate(edges) %>%
  tidygraph::filter(P > 0) %>%
  activate(nodes) %>%
  ggraph(layout = 'circle') +
  geom_node_text(aes(label = name), size = 4,
                 nudge_y = c(-0.15, -0.15, -0.15, -0.15, -0.15)) +
  geom_edge_link2(aes(width = after_stat(index)), color = "grey",
                  alpha = 0.5) +
  geom_node_point(size = 10) +
  scale_edge_width(range = c(0, 5), guide = 'none') +
  coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  theme_void() -> p_lg

ggdraw(p_lg) +
  draw_label("Significance", y = 0.66, size = 14) +
  draw_label("X significantly", y = 0.62, size = 10) +
  draw_label("exceeds Y", y = 0.58, size = 10) +
  draw_label("(p < 0.05)",  y = 0.54, size = 10) -> p_lg

# 网格
grid.arrange(p_gp, p_lg,
             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.

df &lt;- data.frame( 
&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;), 
&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;), 
&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), 
&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?

install.packages(&#39;ragg&#39;)
install.packages(&#39;ggraph&#39;)
library(ggraph)
library(tidygraph)
install.packages(&quot;cowplot&quot;)
library(cowplot)
library(gridExtra)
df &lt;- data.frame( 
&quot;PersonName1&quot; = c(&quot;No Speaker&quot;,&quot;No Speaker&quot;), 
&quot;PersonName2&quot; = c(&quot;Fauci&quot;,&quot;Hanks&quot;), 
&quot;P&quot; = c(0.5, 0) ) 
df %&gt;%
as_tbl_graph() %&gt;%
activate(edges) %&gt;%
tidygraph::filter(P &gt; 0) %&gt;%
activate(nodes) %&gt;%
ggraph(layout = &#39;circle&#39;) +
geom_node_text(aes(label = name), size = 8, 
nudge_x = c(0.2, 0, -0.2, -0.2, 0), 
nudge_y = c(0.3, 0.3, 0.3, -0.3, -0.3),
check_overlap = TRUE) + 
geom_edge_link2(aes(width = after_stat(index)), color = &quot;red&quot;,
alpha = 0.5) +
geom_node_point(size = 20)  +
scale_edge_width(range = c(0, 15), guide = &#39;none&#39;) +
coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
theme_void() -&gt;p_gp
### legend
data.frame(lg1 = &quot;Y&quot;, lg2 = &quot;X&quot; , P = 0.5) %&gt;% 
as_tbl_graph() %&gt;%
activate(edges) %&gt;%
tidygraph::filter(P &gt; 0) %&gt;%
activate(nodes) %&gt;%
ggraph(layout = &#39;circle&#39;) +
geom_node_text(aes(label = name), size = 4,
nudge_y = c(-0.15, -0.15, -0.15, -0.15, -0.15)) +
geom_edge_link2(aes(width = after_stat(index)), color = &quot;grey&quot;,
alpha = 0.5) +
geom_node_point(size = 10) +
scale_edge_width(range = c(0, 5), guide = &#39;none&#39;) +
coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
theme_void() -&gt; p_lg
ggdraw(p_lg) +
draw_label(&quot;Significance&quot;, y = 0.66, size = 14) +
draw_label(&quot;X significantly&quot;, y = 0.62, size = 10) +
draw_label(&quot;exceeds Y&quot;, y = 0.58, size = 10) +
draw_label(&quot;(p &lt; 0.05)&quot;,  y = 0.54, size = 10) -&gt; p_lg
### grid
grid.arrange(p_gp, p_lg,
ncol=2, nrow=1, widths=c(9/10,1/10))

答案1

得分: 1

library(ggraph)
library(tidygraph)
library(patchwork)
library(cowplot)

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

node_names <-
  c("Hanks", "Kardashian", "Fauci", "Government", "No Speaker")
types <- c("Brazil", "South Korea", "Italy")
n <- 500

df <-
  data.frame(
    from = sample(node_names, n, replace = TRUE),
    to = sample(node_names, n, replace = TRUE),
    P = abs(rnorm(n)),
    type = sample(types, n, replace = TRUE)
  ) %>%
  distinct(from, to, .keep_all = TRUE)

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

p_gp <-
  df %>%
  as_tbl_graph() %>%
  activate(edges) %>%
  tidygraph::filter(P > 0) %>%
  activate(nodes) %>%
  ggraph(layout = 'circle') +
  geom_node_text(
    aes(label = name),
    nudge_x = c(0.2, 0,-0.2,-0.2, 0),
    nudge_y = c(0.3, 0.3, 0.3,-0.3,-0.3),
    check_overlap = TRUE
  ) +
  geom_edge_link2(aes(width = after_stat(index), color = type), ,
                  alpha = 0.5) +
  geom_node_point()  +
  scale_edge_width(range = c(0, 2), guide = 'none') +
  scale_edge_color_manual(values = c("green", "blue", "red"), guide = "none") +
  coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  facet_edges( ~ type) +
  theme_void() +
  theme(aspect.ratio = 1) # 确保分面始终为正方形。

### 图例
p_lg <- ...

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

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

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

英文:
library(ggraph)
library(tidygraph)
library(patchwork)
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.

node_names &lt;-
  c(&quot;Hanks&quot;, &quot;Kardashian&quot;, &quot;Fauci&quot;, &quot;Government&quot;, &quot;No Speaker&quot;)
types &lt;- c(&quot;Brazil&quot;, &quot;South Korea&quot;, &quot;Italy&quot;)
n &lt;- 500

df &lt;-
  data.frame(
    from = sample(node_names, n, replace = TRUE),
    to = sample(node_names, n, replace = TRUE),
    P = abs(rnorm(n)),
    type = sample(types, n, replace = TRUE)
  ) |&gt;
  distinct(from, to, .keep_all = TRUE)

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

p_gp &lt;-
  df %&gt;%
  as_tbl_graph() %&gt;%
  activate(edges) %&gt;%
  tidygraph::filter(P &gt; 0) %&gt;%
  activate(nodes) %&gt;%
  ggraph(layout = &#39;circle&#39;) +
  geom_node_text(
    aes(label = name),
    nudge_x = c(0.2, 0,-0.2,-0.2, 0),
    nudge_y = c(0.3, 0.3, 0.3,-0.3,-0.3),
    check_overlap = TRUE
  ) +
  geom_edge_link2(aes(width = after_stat(index), color = type), ,
                  alpha = 0.5) +
  geom_node_point()  +
  scale_edge_width(range = c(0, 2), guide = &#39;none&#39;) +
  scale_edge_color_manual(values = c(&quot;green&quot;, &quot;blue&quot;, &quot;red&quot;), guide = &quot;none&quot;) +
  coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  facet_edges( ~ type) +
  theme_void() +
  theme(aspect.ratio = 1) # this makes sure that the facets are always square.


### legend
p_lg &lt;-
  data.frame(lg1 = &quot;Y&quot;, lg2 = &quot;X&quot; , P = 0.5) %&gt;%
  as_tbl_graph() %&gt;%
  activate(edges) %&gt;%
  tidygraph::filter(P &gt; 0) %&gt;%
  activate(nodes) %&gt;%
  ggraph(layout = &#39;circle&#39;) +
  geom_node_text(
    aes(label = name),
    size = 4,
    nudge_y = c(-0.15,-0.15,-0.15,-0.15,-0.15)
  ) +
  geom_edge_link2(aes(width = after_stat(index)),
                  color = &quot;grey&quot;,
                  alpha = 0.5) +
  geom_node_point(size = 10) +
  scale_edge_width(range = c(0, 5), guide = &#39;none&#39;) +
  coord_cartesian(xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5)) +
  theme_void()

p_lg &lt;-
  ggdraw(p_lg) +
  draw_label(&quot;Significance&quot;, y = 1.2, size = 14) +
  draw_label(&quot;X significantly&quot;, y = 1, size = 10) +
  draw_label(&quot;exceeds Y&quot;, y = .8, size = 10) +
  draw_label(&quot;(p &lt; 0.05)&quot;,  y = .6, size = 10)
#&gt; Warning in y + params$y: longer object length is not a multiple of shorter
#&gt; object length
#&gt; Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
#&gt; ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.
#&gt; This warning is displayed once every 8 hours.
#&gt; Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#&gt; generated.

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

p_gp / p_lg +
  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:

确定