I'm creating a sample ego network plot, but I'd like the nodes to be closer together, specifically, the alter nodes closer to the ego node. My code:
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")
Output:
Given how small the ego network is, I'd like the alters nodes to be closer to ego. Is there a way to shorten the distance between the nodes?
I was able to shorten the distance by changing the x and y axis limits:
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")