rm(list = ls())
###############################input data
#install.packages("ggnetwork")
library(ggplot2)
library(ggnetwork)
###############################input data
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- dir(dir_path,pattern = ".*.csv",full.names = T)
#dir_path_name
#####################################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
###############################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
#head(edge_1)
#####################################___edge
colnames(edge_1) <- c("from","to","edge_size")
edge_1$edge_label <- do.call(paste0, expand.grid(rep(list(letters), 2)))[1:nrow(edge_1)]
edge_1$edge_color <- factor(as.integer(factor(edge_1$edge_size)))
edge_1$edge_type <- sample(letters[1:3],nrow(edge_1),replace = T)
edge_1$edge_color[edge_1$edge_size == 0] <- NA
edge_1$edge_type [edge_1$edge_size == 0] <- NA
edge_1$edge_label[edge_1$edge_size == 0] <- NA
#View(edge_1)
head(edge_1)
## from to edge_size edge_label edge_color
## 1 Nephrotoxicity Reproductive toxicity 6 aa 5
## 2 Cardiotoxicity Nephrotoxicity 3 ba 4
## 3 Cardiotoxicity Reproductive toxicity 3 ca 4
## 4 Nephrotoxicity Skin toxicity 3 da 4
## 5 Developmental toxicity Nephrotoxicity 2 ea 3
## 6 Developmental toxicity Reproductive toxicity 2 fa 3
## edge_type
## 1 c
## 2 a
## 3 c
## 4 c
## 5 b
## 6 b
####################################____node
colnames(node_1) <- c("vertex.names","node_size")
node_1$node_color <- do.call(paste0, expand.grid(rep(list(LETTERS), 2)))[1:nrow(node_1)]
node_1$node_label <- do.call(paste0, expand.grid(rep(list(letters), 2)))[1:nrow(node_1)]
node_1$node_size <- 1:nrow(node_1)
#View(node_1)
head(node_1)
## vertex.names node_size node_color node_label
## 1 Carcinogenicity 1 AA aa
## 2 Cardiotoxicity 2 BA ba
## 3 Developmental toxicity 3 CA ca
## 4 Hepatotoxicity 4 DA da
## 5 Nephrotoxicity 5 EA ea
## 6 Neurotoxicity 6 FA fa
####################################################PLOT
#layout = "kamadakawai" # "fruchtermanreingold" "eigen" "hall" "mds" "princoord" "random" "circle"
data_plot <- ggnetwork(edge_1, layout = "kamadakawai")
#data_plot
data_p <- merge(data_plot, node_1, by = "vertex.names", all.x = T)
data_p[1:10,]
## vertex.names x y xend yend edge_color
## 1 Carcinogenicity 0.04232254 0.7540426 0.465641297 0.50219347 <NA>
## 2 Carcinogenicity 0.04232254 0.7540426 0.976255477 0.44634825 <NA>
## 3 Carcinogenicity 0.04232254 0.7540426 0.002382602 0.33687116 <NA>
## 4 Carcinogenicity 0.04232254 0.7540426 0.821350630 0.85780690 <NA>
## 5 Carcinogenicity 0.04232254 0.7540426 0.750480729 0.07373532 <NA>
## 6 Carcinogenicity 0.04232254 0.7540426 0.395590949 0.98626737 <NA>
## 7 Carcinogenicity 0.04232254 0.7540426 0.313094124 0.02344185 <NA>
## 8 Carcinogenicity 0.04232254 0.7540426 0.042322535 0.75404256 <NA>
## 9 Cardiotoxicity 1.00000000 0.4385254 0.512004289 0.48694281 <NA>
## 10 Cardiotoxicity 1.00000000 0.4385254 0.854685260 0.83761644 2
## edge_label edge_size edge_type node_size node_color node_label
## 1 <NA> 0 <NA> 1 AA aa
## 2 <NA> 0 <NA> 1 AA aa
## 3 <NA> 0 <NA> 1 AA aa
## 4 <NA> 0 <NA> 1 AA aa
## 5 <NA> 0 <NA> 1 AA aa
## 6 <NA> 0 <NA> 1 AA aa
## 7 <NA> 0 <NA> 1 AA aa
## 8 <NA> NA <NA> 1 AA aa
## 9 <NA> 0 <NA> 2 BA ba
## 10 ga 1 c 2 BA ba
#View(data_p)
#colnames(data_p)
#class(data_p)
p1 <- ggplot(data_p,
mapping = aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(aes(linetype = edge_type, color = edge_color, size = edge_size),
curvature = 0, alpha = 0.8, show.legend =F, na.rm = T,
arrow = arrow(length = unit(0, "lines"), type = "closed")) +
geom_edgetext(aes(label = edge_label), color = "red", fill = "grey80") +
geom_edgetext_repel(aes(label = edge_label),
color = "purple", fill = "skyblue",
box.padding = unit(1, "lines"))
p1
## Warning: Removed 19 rows containing missing values (geom_label).
## Warning: Removed 19 rows containing missing values (geom_label_repel).

p2 <- p1 + geom_nodes(aes(color = node_color, size = node_size, fill = node_size),
shape = 21, stroke = 2) +
geom_nodetext(mapping = aes(label = node_label), color = "white") +
geom_nodelabel_repel(mapping = aes(label = node_label), color = "red", fill = "skyblue",
fontface = "bold", box.padding = unit(3, "lines"))
p2
## Warning: Removed 19 rows containing missing values (geom_label).
## Warning: Removed 19 rows containing missing values (geom_label_repel).

#geom_nodetext_repel(data_p, mapping = aes(x = x, y = y, xend = xend, yend = yend,
# label = node_label), color = "black",
# box.padding = unit(5, "lines"))
#scale_size_continuous(range = c(1, 2))
p2
## Warning: Removed 19 rows containing missing values (geom_label).
## Warning: Removed 19 rows containing missing values (geom_label_repel).

p3 <- p2 + theme(axis.text = element_blank(),
axis.title = element_blank(),
panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank(),
plot.margin = unit(c(0,0,1,0), "cm"),
legend.position = "bottom",
legend.key = element_rect(colour = NA, fill = NA)) +
scale_fill_continuous(type = "gradient") +
scale_color_brewer(type = 'div', palette = 'Set3') +
guides(color = "none", linetype = "none",fill = "none")
p3
## Warning: Removed 19 rows containing missing values (geom_label).
## Warning: Removed 19 rows containing missing values (geom_label_repel).

# guides(color =guide_legend(ncol=2), size =guide_legend(ncol=2))
#unique(data_p$node_size)
p3 + scale_size("SIZE",range = c(1, 10), breaks = seq(1,8,2))
## Warning: Removed 19 rows containing missing values (geom_label).
## Warning: Removed 19 rows containing missing values (geom_label_repel).

ggsave(filename = paste0(Sys.Date(),"network.tif"),
plot = last_plot(), device = "tiff", path = dir_path,
width = 18, height = 18, units = "cm",
dpi = 300, limitsize = TRUE)
## Warning: Removed 19 rows containing missing values (geom_label).
## Warning: Removed 19 rows containing missing values (geom_label_repel).
#scale_color_brewer("Conference",
# palette = "Paired")
###ref https://cran.r-project.org/web/packages/ggnetwork/vignettes/ggnetwork.html