rm(list = ls())
###############################input data
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- list.files(pattern = ".*xlsx",dir_path,full.names = T, recursive = F)
#dir_path_name
library(openxlsx)
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()
#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)
data_2 <- data.frame(sm = unlist(strsplit(data_1$source_specific, ",")),
gm = unlist(strsplit(data_1$source_general, ",")))
data_2$sm <- gsub("_.*", "", data_2$sm)
data_2$sm <- trimws(data_2$sm)
data_2$gm <- gsub("_.*", "", data_2$gm)
data_2$gm <- trimws(data_2$gm)
#View(data_2)
data_2$level1 <- 1:nrow(data_2)
#######################################
library(tidyverse)
data_3 <- gather(data_2 , key = level2, value = level3, -3)
data_3$level1 <- "boss"
data_3$level2 <- gsub("sm", "Specific", data_3$level2)
data_3$level2 <- gsub("gm", "General", data_3$level2)
data_3
## level1 level2 level3
## 1 boss Specific A
## 2 boss Specific B
## 3 boss Specific C
## 4 boss Specific D
## 5 boss General A
## 6 boss General B
## 7 boss General C
## 8 boss General D
######################################
edges_level1_2 = data_3 %>% select(level1, level2) %>% unique %>% rename(from=level1, to=level2)
edges_level2_3 = data_3 %>% select(level2, level3) %>% unique %>% rename(from=level2, to=level3)
edge_list=rbind(edges_level1_2, edges_level2_3)
edge_list
## from to
## 1 boss Specific
## 5 boss General
## 11 Specific A
## 2 Specific B
## 3 Specific C
## 4 Specific D
## 51 General A
## 6 General B
## 7 General C
## 8 General D
mygraph <- graph_from_data_frame( edge_list )
ggraph(mygraph, layout = 'dendrogram', circular = FALSE) +
geom_edge_diagonal() +
geom_node_point() +
theme_void()
## Multiple parents. Unfolding graph

ltdf = create_layout(mygraph, layout = 'dendrogram')
## Multiple parents. Unfolding graph
ggraph(ltdf, layout = 'dendrogram', circular = FALSE) +
geom_edge_diagonal(edge_colour = "skyblue", edge_width = 1)+
geom_node_label(aes(label = name)) + # name is a column in the layout data frame
theme_void()

ltdf = create_layout(mygraph, layout = 'tree')
ltdf$x[4:7] <- c(-1,-0.33,0.33,1)
ggraph(ltdf, layout = 'tree', circular = FALSE) +
geom_edge_diagonal(color = "gray80", edge_width = .7) +
geom_node_label(aes(label = name, color = name), alpha = 1, fill = "white") + # name is a column in the layout data frame
theme(plot.margin= unit(c(0.1,.1,.1,.1),"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.1,0.1))

ggsave(filename = paste0(Sys.Date(),"sars_tree1.tiff"),
plot = last_plot(), device = "tiff", path = dir_path,
width = 8, height = 8, units = "cm",
dpi = 300, limitsize = TRUE)