for report : adding baldock network generation
This commit is contained in:
parent
632c88684a
commit
8a4506e956
1 changed files with 117 additions and 0 deletions
117
code/for_report/applications/baldock.R
Normal file
117
code/for_report/applications/baldock.R
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
library("ggplot2")
|
||||
library("ggokabeito")
|
||||
library("tidyr")
|
||||
library("dplyr")
|
||||
library("stringr")
|
||||
library("knitr")
|
||||
library("kableExtra")
|
||||
library("stringr")
|
||||
library("here")
|
||||
library("tikzDevice")
|
||||
library("network")
|
||||
library("GGally")
|
||||
library("igraph")
|
||||
library("tidygraph")
|
||||
library("ggraph")
|
||||
|
||||
base_data_folder <- file.path(here(), "code", "data", "dore")
|
||||
save_folder <- file.path(
|
||||
here(), "mia-rapport-2024", "results", "applications",
|
||||
"sub-dore", "baldock"
|
||||
)
|
||||
|
||||
if (!dir.exists(save_folder)) {
|
||||
dir.create(save_folder, recursive = TRUE)
|
||||
}
|
||||
|
||||
collection_data <- file.path(
|
||||
base_data_folder,
|
||||
"dore-matrices.Rds"
|
||||
)
|
||||
|
||||
interaction_data <- read.table(file = file.path(base_data_folder, "interaction-data.txt"), sep = "\t", header = TRUE)
|
||||
|
||||
seq_ids_network_aggreg <- unique(interaction_data$id_network_aggreg)
|
||||
names_aggreg_networks <- sapply(
|
||||
seq_ids_network_aggreg,
|
||||
function(id) {
|
||||
paste0(
|
||||
unique(interaction_data[which(interaction_data$id_network_aggreg == id), ]$web),
|
||||
collapse = "+"
|
||||
)
|
||||
}
|
||||
)
|
||||
# Computation of incidence matrices
|
||||
incidence_matrices <- lapply(
|
||||
seq_ids_network_aggreg,
|
||||
function(m) {
|
||||
current_interaction_data <- interaction_data[which(interaction_data$id_network_aggreg == m), ] %>%
|
||||
mutate(
|
||||
plantaggreg = paste(plantorder,
|
||||
plantfamily, plantgenus, plantspecies,
|
||||
sep = "-"
|
||||
),
|
||||
insectaggreg = paste(insectorder,
|
||||
insectfamily, insectgenus, insectspecies,
|
||||
sep = "-"
|
||||
)
|
||||
)
|
||||
current_interaction_data <- table(current_interaction_data$plantaggreg, current_interaction_data$insectaggreg)
|
||||
|
||||
current_incidence_matrix <- matrix(current_interaction_data,
|
||||
ncol = ncol(current_interaction_data), dimnames = dimnames(current_interaction_data)
|
||||
)
|
||||
|
||||
current_incidence_matrix[which(current_incidence_matrix > 0)] <- 1
|
||||
return(current_incidence_matrix)
|
||||
}
|
||||
)
|
||||
|
||||
names(incidence_matrices) <- names_aggreg_networks
|
||||
|
||||
incidence_matrices <- incidence_matrices[grepl(
|
||||
x = names(incidence_matrices),
|
||||
pattern = "Baldock"
|
||||
)]
|
||||
|
||||
oi_palette <- palette_okabe_ito(1:2)
|
||||
names(oi_palette) <- c("plant", "insect")
|
||||
|
||||
set.seed(1234)
|
||||
|
||||
inc_mat <- incidence_matrices[[1]]
|
||||
aaa <- incidence_matrices[[2]]
|
||||
|
||||
plot_list <- lapply(seq_along(incidence_matrices), function(id) {
|
||||
incidence_matrices[[id]] |>
|
||||
as.matrix() |>
|
||||
as_tbl_graph() |>
|
||||
mutate(node_type = ifelse(type, "Insect", "Plant"), graph_id = id) |>
|
||||
ggraph(layout = "igraph", algorithm = "bipartite") +
|
||||
geom_node_point(aes(color = node_type)) +
|
||||
geom_edge_link(alpha = 0.1) +
|
||||
scale_color_okabe_ito(order = 6:7) +
|
||||
theme_void() +
|
||||
labs(color = "Node type") +
|
||||
theme(legend.position = "bottom")
|
||||
})
|
||||
names(plot_list) <- names(incidence_matrices)
|
||||
|
||||
output_tikz_folder <- here(
|
||||
"mia-rapport-2024", "tikz", "applications",
|
||||
"baldock"
|
||||
)
|
||||
if (!dir.exists(output_tikz_folder)) {
|
||||
dir.create(output_tikz_folder, recursive = TRUE)
|
||||
}
|
||||
options(tikzDocumentDeclaration = "\\documentclass[10pt]{standalone}")
|
||||
lapply(seq_along(plot_list), function(id) {
|
||||
tikz(
|
||||
file = file.path(output_tikz_folder, paste0("graph-", names(plot_list)[[id]], ".tex")),
|
||||
width = 3,
|
||||
height = 1,
|
||||
standAlone = TRUE
|
||||
)
|
||||
print(plot_list[[id]])
|
||||
dev.off()
|
||||
})
|
||||
Loading…
Add table
Reference in a new issue