rm(list = ls())
library(ggraph); library(igraph); library(tidyverse)
## 载入需要的程辑包:ggplot2
##
## 载入程辑包:'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## v purrr 0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::as_data_frame() masks tibble::as_data_frame(), igraph::as_data_frame()
## x purrr::compose() masks igraph::compose()
## x tidyr::crossing() masks igraph::crossing()
## x dplyr::filter() masks stats::filter()
## x dplyr::groups() masks igraph::groups()
## x dplyr::lag() masks stats::lag()
## x purrr::simplify() masks igraph::simplify()
###############################input data
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\graph_igrapg_network_\\"
dir_path_name <- list.files(pattern = ".*xlsx",dir_path,full.names = T, recursive = T)
#dir_path_name
library(openxlsx)
#getSheetNames(grep("data.xlsx",dir_path_name, value = T))
data_1 <- read.xlsx(grep("data.xlsx",dir_path_name, value = T), sheet = 1,startRow = 1)
#View(data_1)
data_1$Subchallenge.Assay <- gsub("NR-","", data_1$Subchallenge.Assay)
data_1$Subchallenge.Assay <- gsub("SR-","", data_1$Subchallenge.Assay)
#############################################################################
library(ggplot2)
library(RColorBrewer)
#head(data_1,2)
#unique(data_1$Method)
colnames(data_1) <- c("Assay","Team","AUC","Method")
data_1$Method <- gsub("\\(.*", "", data_1$Method)
data_1$Method <- factor(data_1$Method)
data_1$Method <- trimws(data_1$Method)
data_1$Method <- gsub(" ", "\n", data_1$Method)
data_1$Method <- gsub("Associative\nneural\nnetworks", "Associative\nneural network",data_1$Method)
##################################################
head(data_1)
## Assay Team AUC Method
## 1 AhR Bioinf@JKU 0.93 Deep\nLearning
## 2 AR dmlab 0.83 Multi-tree\nensemble
## 3 AR-LBD Bioinf@JKU-ensemble3 0.88 Deep\nLearning
## 4 Aromatase dmlab 0.84 Multi-tree\nensemble
## 5 ER Bioinf@JKU-ensemble1 0.81 Deep\nLearning
## 6 ER-LBD microsomes 0.83 Random\nForest
edge_1 <- data_1[, c(4,1)] # 2nd edge
colnames(edge_1) <- c("from", "to")
data_2 <- edge_1 #1st edge
data_2$to <- "In vitro toxicity"
colnames(data_2) <- rev(c("from", "to"))
edge_all <- rbind(edge_1, data_2)
edge_all$from <- gsub("In vitro toxicity", "In vitro\ntoxicity", edge_all$from)
#####################################
name <- data.frame(unique(c(as.character(edge_all$from), as.character(edge_all$to))))
#head(name)
colnames(name) <- "Assay"
name_1 <- merge(name, data_1, by = "Assay", all.x = T)
#View(name_1)
name_1$Team <- name_1$Method <- NULL
#########################################
# Create a graph object
mygraph <- graph_from_data_frame(edge_all, vertices=name_1)
mygraph_1 <- create_layout(mygraph, layout = 'dendrogram')
mygraph_1$leaf_1 <- as.logical(TRUE)
mygraph_1$leaf_2 <- !mygraph_1$leaf
#head(mygraph_1)
#min(mygraph_1$AUC)
#max(mygraph_1$AUC)
#View(mygraph_1)
#View(mygraph_1)
mygraph_1$name <- LETTERS[1:length(mygraph_1$name)]
mygraph_1
## x y leaf name AUC .ggraph.orig_index circular .ggraph.index leaf_1
## 1 6.75 2 FALSE A NA 14 FALSE 1 TRUE
## 2 0.50 1 FALSE B NA 1 FALSE 2 TRUE
## 3 2.50 1 FALSE C NA 7 FALSE 3 TRUE
## 4 6.50 1 FALSE D NA 9 FALSE 4 TRUE
## 5 11.00 1 FALSE E NA 16 FALSE 5 TRUE
## 6 13.00 1 FALSE F NA 20 FALSE 6 TRUE
## 7 4.00 0 TRUE G 0.93 2 FALSE 7 TRUE
## 8 10.00 0 TRUE H 0.83 3 FALSE 8 TRUE
## 9 5.00 0 TRUE I 0.88 4 FALSE 9 TRUE
## 10 6.00 0 TRUE J 0.84 5 FALSE 10 TRUE
## 11 11.00 0 TRUE K 0.84 6 FALSE 11 TRUE
## 12 2.00 0 TRUE L 0.83 8 FALSE 12 TRUE
## 13 7.00 0 TRUE M 0.81 10 FALSE 13 TRUE
## 14 13.00 0 TRUE N 0.83 11 FALSE 14 TRUE
## 15 0.00 0 TRUE O 0.87 12 FALSE 15 TRUE
## 16 8.00 0 TRUE P 0.86 13 FALSE 16 TRUE
## 17 3.00 0 TRUE Q 0.95 15 FALSE 17 TRUE
## 18 12.00 0 TRUE R 0.88 17 FALSE 18 TRUE
## 19 1.00 0 TRUE S 0.92 18 FALSE 19 TRUE
## 20 9.00 0 TRUE T 0.86 19 FALSE 20 TRUE
## leaf_2
## 1 TRUE
## 2 TRUE
## 3 TRUE
## 4 TRUE
## 5 TRUE
## 6 TRUE
## 7 FALSE
## 8 FALSE
## 9 FALSE
## 10 FALSE
## 11 FALSE
## 12 FALSE
## 13 FALSE
## 14 FALSE
## 15 FALSE
## 16 FALSE
## 17 FALSE
## 18 FALSE
## 19 FALSE
## 20 FALSE
ggraph(mygraph_1, layout = 'dendrogram',size = 5) +
geom_edge_diagonal(color = "gray50", alpha=0.5, edge_width = .7) +
#geom_node_point(aes(filter=leaf_2), shape=23, fill="blue", color="blue", size=2, alpha=0.5) +
geom_node_label(aes(label=name, filter=leaf_2), alpha=1, fill = "white", angle=0 , hjust=0.5, nudge_y= 0.01, show.legend = F) +
geom_node_text(aes(label=name, filter=leaf), angle=45 , hjust=1, vjust = 0, nudge_y=-0.1, show.legend = F) +
geom_node_point(aes(filter=leaf, size = AUC, color = name), alpha=1) +
guides(fill=guide_legend(title="name"), color=FALSE) +
scale_size(limits = c(0.8,1), name= "AUC-ROC") + theme(legend.position="right",
plot.background = element_blank())+
theme(plot.margin= unit(c(0,0,0,0),"cm"),
#legend.position = "",
plot.title = element_text(hjust = 0.5),
legend.title = element_text(colour="black",size= 12,
face="plain",family = "sans"),
legend.text=element_text(colour="black",size= 11,
face="plain",family = "sans"),
#legend.direction = "vertical",
#legend.justification = c(0,1),
#panel.background = element_blank(),
panel.background=element_rect(fill=NA),
#panel.background = element_rect(fill = "blue",
# colour = "red",
# size = 0.5, linetype = "solid"),
axis.ticks = element_blank(),
axis.ticks.length.y = unit(0, "mm"),
axis.text.y = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.background = element_blank(),
panel.grid = element_blank(),
plot.background = element_rect(fill = NA),
legend.key = element_rect(colour = NA, fill = NA)) +
scale_x_discrete(expand = c(0.07,0.07))+
scale_y_discrete(expand = c(0.15,0.15))
## Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
## "none")` instead.

###################################################
ggsave(paste0(Sys.Date(),"-model_ggraph3.tiff"),
plot = last_plot(), device = NULL,
path = dir_path,
scale = 1, width = 20.6, height = 12, units ="cm",dpi = 300,
limitsize = TRUE,compression = "lzw")
#https://stackoverflow.com/questions/60896935/removing-one-of-two-ggplot-legends
#