rm(list = ls())
###############################input data 
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- dir(dir_path,pattern = ".*.csv",full.names = T)
dir_path_name
## [1] "C:\\Users\\liyix\\OneDrive\\Desktop\\pathways_number_stat_edge.csv"
## [2] "C:\\Users\\liyix\\OneDrive\\Desktop\\pathways_number_stat_node.csv"
#####################################node
node_1 <- read.csv(grep("pathways_number_stat_node.csv",dir_path_name,value = T),header = T,stringsAsFactors = F)
dim(node_1) #[1] 8 2
## [1] 8 2
node_1$cate <- "pathway"
node_1
##                     Var1 Freq    cate
## 1        Carcinogenicity   51 pathway
## 2         Cardiotoxicity   20 pathway
## 3 Developmental toxicity    3 pathway
## 4         Hepatotoxicity   11 pathway
## 5         Nephrotoxicity   12 pathway
## 6          Neurotoxicity   13 pathway
## 7  Reproductive toxicity   12 pathway
## 8          Skin toxicity  101 pathway
#View(node_1)
colnames(node_1) <- c("name","Count","ONTOLOGY")
###############################edge
edge_1 <- read.csv(grep("pathways_number_stat_edge.csv",dir_path_name,value = T),header = T,stringsAsFactors = F)
dim(edge_1) #[1] 28  3
## [1] 28  3
colnames(edge_1) <- c("source","target","weight")
summary(edge_1)
##     source             target              weight      
##  Length:28          Length:28          Min.   :0.0000  
##  Class :character   Class :character   1st Qu.:0.0000  
##  Mode  :character   Mode  :character   Median :0.0000  
##                                        Mean   :0.7857  
##                                        3rd Qu.:1.0000  
##                                        Max.   :6.0000
edge_1
##                    source                 target weight
## 1          Nephrotoxicity  Reproductive toxicity      6
## 2          Cardiotoxicity         Nephrotoxicity      3
## 3          Cardiotoxicity  Reproductive toxicity      3
## 4          Nephrotoxicity          Skin toxicity      3
## 5  Developmental toxicity         Nephrotoxicity      2
## 6  Developmental toxicity  Reproductive toxicity      2
## 7          Cardiotoxicity Developmental toxicity      1
## 8          Cardiotoxicity          Skin toxicity      1
## 9   Reproductive toxicity          Skin toxicity      1
## 10        Carcinogenicity         Cardiotoxicity      0
## 11        Carcinogenicity Developmental toxicity      0
## 12        Carcinogenicity         Hepatotoxicity      0
## 13        Carcinogenicity         Nephrotoxicity      0
## 14        Carcinogenicity          Neurotoxicity      0
## 15        Carcinogenicity  Reproductive toxicity      0
## 16        Carcinogenicity          Skin toxicity      0
## 17         Cardiotoxicity         Hepatotoxicity      0
## 18         Cardiotoxicity          Neurotoxicity      0
## 19 Developmental toxicity         Hepatotoxicity      0
## 20 Developmental toxicity          Neurotoxicity      0
## 21 Developmental toxicity          Skin toxicity      0
## 22         Hepatotoxicity         Nephrotoxicity      0
## 23         Hepatotoxicity          Neurotoxicity      0
## 24         Hepatotoxicity  Reproductive toxicity      0
## 25         Hepatotoxicity          Skin toxicity      0
## 26         Nephrotoxicity          Neurotoxicity      0
## 27          Neurotoxicity  Reproductive toxicity      0
## 28          Neurotoxicity          Skin toxicity      0
###########################################################plot
#install.packages("igraph")
library(igraph)
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
g <- graph_from_data_frame(d=edge_1, vertices=node_1, directed=FALSE)
#str(g)
show(g)
## IGRAPH 1904662 UNW- 8 28 -- 
## + attr: name (v/c), Count (v/n), ONTOLOGY (v/c), weight (e/n)
## + edges from 1904662 (vertex names):
##  [1] Nephrotoxicity        --Reproductive toxicity 
##  [2] Cardiotoxicity        --Nephrotoxicity        
##  [3] Cardiotoxicity        --Reproductive toxicity 
##  [4] Nephrotoxicity        --Skin toxicity         
##  [5] Developmental toxicity--Nephrotoxicity        
##  [6] Developmental toxicity--Reproductive toxicity 
##  [7] Cardiotoxicity        --Developmental toxicity
##  [8] Cardiotoxicity        --Skin toxicity         
## + ... omitted several edges
#View(edge_1)
#tiff(filename=paste0(dir_path,"network_1.tif"),width = 20, height = 20, units = "cm", 
#    compression = "lzw",bg = "white", res = 300, family = "serif", restoreConsole = TRUE)

p1 <- plot(g, layout=layout_in_circle, main="",vertex.label.color="black", 
           vertex.label.dist=0, vertex.label.font =1,edge.curved=0,
           vertex.label.cex = 1.5,vertex.label.family = "serif",
           edge.color="gray70", vertex.color = categorical_pal(8))

legend(x =0, y = -1.2, legend=c("3   ", "200 "), pch = 21, bty = "n",col = "black",
       pt.cex = c(7,9),horiz= T,  x.intersp = 2.5,adj = 2,cex=1.5) 
legend(x = -2.2, y = -1.2, legend=c("Number of pathways"), 
       pch=21, pt.bg="black", pt.cex=0, bty="n",horiz=TRUE,cex=1.5)

#dev.off()




########################################
par(mfrow=c(2, 3), mar=c(0,0,1,0))
plot(g, layout=layout_randomly, main="Random")
plot(g, layout=layout_in_circle, main="Circle")
plot(g, layout=layout_as_star, main="Star")
plot(g, layout=layout_as_tree, main="Tree")
plot(g, layout=layout_on_grid, main="Grid")
plot(g, layout=layout_with_fr, main="Force-directed")

#################################################category color_plot
#V(g)$size <- log(strength(g))
V(g)$size <- node_1$Count
E(g)$width <- edge_1$weight
#abs(scale(edge_1$weight))
#par(mar=c(.2,0.2,.2,.2))
V(g)$name
## [1] "Carcinogenicity"        "Cardiotoxicity"         "Developmental toxicity"
## [4] "Hepatotoxicity"         "Nephrotoxicity"         "Neurotoxicity"         
## [7] "Reproductive toxicity"  "Skin toxicity"
#dev.off()

#View(edge)
######################
p1
## NULL