英文:
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 <- 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"))
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('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
### legend
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
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))
英文:
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 <-
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)
Now we use ggraph::facet_edges()
to create three separate plots/facets.
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) # this makes sure that the facets are always square.
### legend
p_lg <-
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 = 1.2, size = 14) +
draw_label("X significantly", y = 1, size = 10) +
draw_label("exceeds Y", y = .8, size = 10) +
draw_label("(p < 0.05)", y = .6, size = 10)
#> Warning in y + params$y: longer object length is not a multiple of shorter
#> object length
#> Warning: Using the `size` aesthetic in this geom was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` in the `default_aes` field and elsewhere instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
To add in the custom legend we can use the patchwork
package.
p_gp / p_lg +
plot_layout(heights = c(5, 1))
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论