Skip to content

Instantly share code, notes, and snippets.

@pratikunterwegs
Last active July 5, 2023 21:34
Show Gist options
  • Save pratikunterwegs/e1e2e34c1cd54e0d9e5f164b9891eeb2 to your computer and use it in GitHub Desktop.
Save pratikunterwegs/e1e2e34c1cd54e0d9e5f164b9891eeb2 to your computer and use it in GitHub Desktop.
Plotting the Epiverse-TRACE interaction network
library(tidyverse)
library(readxl)
library(tidygraph)
library(scico)
library(ggraph)
# read data on connections
df <- read_excel("contact_matrix.xlsx",
sheet = "matrix"
)
df <- rename(df, name = `...1`)
# read affiliations
affils <- read_excel(
"contact_matrix.xlsx",
sheet = "affils"
)
# rename columns
affils <- rename(affils, name = Name, institution = Institution)
# change MRC to MRC-The Gambia
affils <- mutate(
affils,
institution = if_else(institution == "MRC", "MRC-The Gambia", institution)
)
# pivot longer to make edgelist
df <- pivot_longer(
data = df,
!name,
names_to = "name2",
values_to = "weight"
)
# rename for tidygraph and remove last names
df <- rename(df, from = name, to = name2)
# remove NA weights
df <- filter(df, !is.na(weight))
# make graph object
g <- tidygraph::as_tbl_graph(df, directed = TRUE)
# add affiliations
g <- activate(g, "nodes") %>%
left_join(affils) %>%
mutate(
initial_last = str_extract(name, "\\s[^ ]+$") %>%
str_extract("\\w{1}"),
name_print = str_replace(
name, "\\s[^ ]+$", sprintf(" %s.", initial_last)
)
)
# calculate centrality
g <- activate(g, "nodes") %>%
mutate(
centrality = tidygraph::centrality_betweenness()
)
# add institution to edges
affils_new <- as_tibble(g) %>%
mutate(
from = seq(nrow(affils_new))
)
g <- activate(g, "edges") %>%
left_join(
select(affils_new, from, institution, name)
) %>%
activate("nodes")
# plot with ggraph
plot <-
ggraph(graph = g, layout = "stress") +
geom_edge_fan(
aes(
edge_alpha = as.factor(weight),
edge_colour = as.factor(institution),
),
edge_width = 0.7,
lineend = "round"
) +
geom_node_point(
aes(
fill = institution,
size = centrality
),
shape = 21, colour = "white"
) +
geom_node_label(
aes(
label = name_print
),
colour = "grey30",
size = 3,
fill = alpha("grey", 0.6),
repel = TRUE
) +
scale_fill_scico_d(
palette = "hawaii",
name = "Institution",
na.value = "grey"
) +
scale_edge_color_manual(
values = scico(5, palette = "hawaii"),
name = "Institution",
na.value = "grey"
) +
scale_size(
range = c(2, 10)
) +
scale_edge_alpha_manual(
values = c(
"1" = 0.1,
"2" = 0.3,
"3" = 0.5,
"4" = 0.8,
"5" = 1
),
labels = c(
"1" = "Rarely",
"2" = "Seldom",
"3" = "Sometimes",
"4" = "Often",
"5" = "Very often"
)
) +
guides(
size = "none",
edge_colour = "none",
edge_alpha = guide_legend(
title = "Interactions",
nrow = 2
),
fill = guide_legend(
nrow = 2,
override.aes = list(
size = 5
)
)
) +
theme_void() +
theme(
plot.background = element_rect(
fill = "white"
),
legend.position = "top"
)
# save plot
ggsave(
plot,
filename = "fig_epiverse_network.png",
dpi = 300,
width = 8, height = 6
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment