R ggraph:减少节点之间的距离
R ggraph: Decrease distance between nodes
我正在创建一个样本自我网络图,但我希望节点靠得更近,具体来说,更接近自我节点的改变节点。我的代码:
library(tidyverse)
library(igraph)
library(gridExtra)
library(ggraph)
library(tidygraph)
library(ggplot2)
library(graphlayouts)
edges <- read.table(text =
"ego wave fid1 fid2 fid3 fid4 fid5
Ego 1 Friend_A Friend_B Friend_C NA Friend_D
Ego 2 Friend_E Friend_F NA NA Friend_G
Ego 3 Friend_H NA Friend_I Friend_G Friend_J
Ego 4 Friend_H NA NA NA NA
Ego 5 Friend_K NA NA NA Friend_F", header = TRUE) %>%
mutate_all(function(x) gsub("_"," ",x)) %>%
pivot_longer(.,
cols = c(fid1:fid5)) %>%
select(., ego, alter = value, wave) %>% na.omit()
ego <- as.data.frame(edges$ego) %>%
rename("id" = "edges$ego")
alter <- as.data.frame(edges$alter) %>%
rename("id" = "edges$alter")
nodes <- bind_rows(ego, alter) %>% distinct() %>%
mutate(label = case_when(id == "Ego" ~ 1,
TRUE ~ 0))
g1 <- graph_from_data_frame(d = filter(edges, wave == 1), vertices = nodes, directed = TRUE) %>%
delete.vertices(., which(degree(.)==0))
as_tbl_graph(g1) %>%
create_layout(., layout = 'kk') %>%
ggraph(.) +
geom_edge_link(color = "grey",
arrow = arrow(type = "closed",
angle = 25,
length = unit(1.5, 'mm')),
end_cap = circle(3.5, 'mm'),
width = 0.5, show.legend = FALSE) +
geom_node_point(aes(fill = factor(label)), , shape = 21, size = 7, color = "black") +
scale_fill_hue(l=40) +
geom_node_text(aes(label = name), vjust = -1.5, hjust = 0.5) +
theme_graph()+
theme(legend.position = "none")+
labs(title = "Wave 1")
输出:
考虑到自我网络有多小,我希望改变节点更接近自我。有没有办法缩短节点之间的距离?
我能够通过更改 x 轴和 y 轴限制来缩短距离:
as_tbl_graph(g1) %>%
create_layout(., layout = 'kk') %>%
ggraph(.) +
geom_edge_link(color = "grey",
arrow = arrow(type = "closed",
angle = 25,
length = unit(1.5, 'mm')),
end_cap = circle(3.5, 'mm'),
width = 0.5, show.legend = FALSE) +
geom_node_point(aes(fill = factor(label)), , shape = 21, size = 7, color = "black") +
scale_fill_hue(l=40) +
geom_node_text(aes(label = name), vjust = -1.5, hjust = 0.5) +
ylim(-3, 3) +
xlim(-3, 3) +
theme_graph()+
theme(legend.position = "none")+
labs(title = "Wave 1")
我正在创建一个样本自我网络图,但我希望节点靠得更近,具体来说,更接近自我节点的改变节点。我的代码:
library(tidyverse)
library(igraph)
library(gridExtra)
library(ggraph)
library(tidygraph)
library(ggplot2)
library(graphlayouts)
edges <- read.table(text =
"ego wave fid1 fid2 fid3 fid4 fid5
Ego 1 Friend_A Friend_B Friend_C NA Friend_D
Ego 2 Friend_E Friend_F NA NA Friend_G
Ego 3 Friend_H NA Friend_I Friend_G Friend_J
Ego 4 Friend_H NA NA NA NA
Ego 5 Friend_K NA NA NA Friend_F", header = TRUE) %>%
mutate_all(function(x) gsub("_"," ",x)) %>%
pivot_longer(.,
cols = c(fid1:fid5)) %>%
select(., ego, alter = value, wave) %>% na.omit()
ego <- as.data.frame(edges$ego) %>%
rename("id" = "edges$ego")
alter <- as.data.frame(edges$alter) %>%
rename("id" = "edges$alter")
nodes <- bind_rows(ego, alter) %>% distinct() %>%
mutate(label = case_when(id == "Ego" ~ 1,
TRUE ~ 0))
g1 <- graph_from_data_frame(d = filter(edges, wave == 1), vertices = nodes, directed = TRUE) %>%
delete.vertices(., which(degree(.)==0))
as_tbl_graph(g1) %>%
create_layout(., layout = 'kk') %>%
ggraph(.) +
geom_edge_link(color = "grey",
arrow = arrow(type = "closed",
angle = 25,
length = unit(1.5, 'mm')),
end_cap = circle(3.5, 'mm'),
width = 0.5, show.legend = FALSE) +
geom_node_point(aes(fill = factor(label)), , shape = 21, size = 7, color = "black") +
scale_fill_hue(l=40) +
geom_node_text(aes(label = name), vjust = -1.5, hjust = 0.5) +
theme_graph()+
theme(legend.position = "none")+
labs(title = "Wave 1")
输出:
考虑到自我网络有多小,我希望改变节点更接近自我。有没有办法缩短节点之间的距离?
我能够通过更改 x 轴和 y 轴限制来缩短距离:
as_tbl_graph(g1) %>%
create_layout(., layout = 'kk') %>%
ggraph(.) +
geom_edge_link(color = "grey",
arrow = arrow(type = "closed",
angle = 25,
length = unit(1.5, 'mm')),
end_cap = circle(3.5, 'mm'),
width = 0.5, show.legend = FALSE) +
geom_node_point(aes(fill = factor(label)), , shape = 21, size = 7, color = "black") +
scale_fill_hue(l=40) +
geom_node_text(aes(label = name), vjust = -1.5, hjust = 0.5) +
ylim(-3, 3) +
xlim(-3, 3) +
theme_graph()+
theme(legend.position = "none")+
labs(title = "Wave 1")