rm(list = ls())
################# ##############input data 
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\original\\"
dirs_list <- list.dirs(dir_path,full.names = T, recursive = F)
data_list_all <- list()
for (i in 1:length(dirs_list)) {
  #i = 1
  files_list <- dir(dirs_list[i],pattern = "*.data_for_plot.csv",full.names = T)
  files_list
  data_list <- list()
  for (j in 1:length(files_list)) {
    #j = 8
    data_1 <- read.csv(files_list[j],header = T,stringsAsFactors = F)
    #View(data_1)
    #unique(data_1$label)
    #AUTONOMIC_GANGLIA
    data_2 <- data_1[ data_1$label != "ALL" & data_1$label != "bb", ]
    cor_r <- cor(data_2$x,c(1:length(data_2$x)),use = "everything",
                 method = c("pearson"))
    data_3 <- data.frame(cor_r, data_2$label[1],gsub("-data_for_plot.csv","",gsub(".*complete-","",files_list[j])))
    colnames(data_3) <- c("Correlation coefficient", "Cancer type", "ttest with p value")
    data_list[[j]] <-  data_3
  }
  data_4 <- do.call("rbind",data_list)
  data_list_all[[i]] <- data_4
}
data_5 <- do.call("rbind", data_list_all)
dim(data_5)
## [1] 200   3
View(data_5)
data_5$pvale <- data_5$`ttest with p value`
head(data_5)
##   Correlation coefficient       Cancer type      ttest with p value
## 1               0.7584686 AUTONOMIC_GANGLIA AUTONOMIC_GANGLIA-0.001
## 2               0.8101534 AUTONOMIC_GANGLIA  AUTONOMIC_GANGLIA-0.01
## 3               0.8088314 AUTONOMIC_GANGLIA  AUTONOMIC_GANGLIA-0.05
## 4               1.0000000 AUTONOMIC_GANGLIA AUTONOMIC_GANGLIA-1e-04
## 5               1.0000000 AUTONOMIC_GANGLIA AUTONOMIC_GANGLIA-1e-05
## 6               1.0000000 AUTONOMIC_GANGLIA AUTONOMIC_GANGLIA-1e-06
##                     pvale
## 1 AUTONOMIC_GANGLIA-0.001
## 2  AUTONOMIC_GANGLIA-0.01
## 3  AUTONOMIC_GANGLIA-0.05
## 4 AUTONOMIC_GANGLIA-1e-04
## 5 AUTONOMIC_GANGLIA-1e-05
## 6 AUTONOMIC_GANGLIA-1e-06
data_5$pvale <- sub(".*?-", "", data_5$pvale)
###########################
str(data_5)
## 'data.frame':    200 obs. of  4 variables:
##  $ Correlation coefficient: num  0.758 0.81 0.809 1 1 ...
##  $ Cancer type            : chr  "AUTONOMIC_GANGLIA" "AUTONOMIC_GANGLIA" "AUTONOMIC_GANGLIA" "AUTONOMIC_GANGLIA" ...
##  $ ttest with p value     : chr  "AUTONOMIC_GANGLIA-0.001" "AUTONOMIC_GANGLIA-0.01" "AUTONOMIC_GANGLIA-0.05" "AUTONOMIC_GANGLIA-1e-04" ...
##  $ pvale                  : chr  "0.001" "0.01" "0.05" "1e-04" ...
data_6 <- data_5
data_5$pvale <- as.numeric(data_5$pvale)
##sort by `Cancer type` (ascending) and pvale (descending)
data_6 <- data_5[order(data_5$`Cancer type`, -data_5$pvale),]
write.csv(data_6, paste0(dir_path,Sys.Date(),"-discribution_each_subject.csv"),row.names = F)
#########################
library(ggplot2)
head(data_6)
##   Correlation coefficient       Cancer type      ttest with p value pvale
## 3               0.8088314 AUTONOMIC_GANGLIA  AUTONOMIC_GANGLIA-0.05 5e-02
## 2               0.8101534 AUTONOMIC_GANGLIA  AUTONOMIC_GANGLIA-0.01 1e-02
## 1               0.7584686 AUTONOMIC_GANGLIA AUTONOMIC_GANGLIA-0.001 1e-03
## 4               1.0000000 AUTONOMIC_GANGLIA AUTONOMIC_GANGLIA-1e-04 1e-04
## 5               1.0000000 AUTONOMIC_GANGLIA AUTONOMIC_GANGLIA-1e-05 1e-05
## 6               1.0000000 AUTONOMIC_GANGLIA AUTONOMIC_GANGLIA-1e-06 1e-06
ggplot(data=data_6 , aes(x=pvale, y=`Correlation coefficient`, group=`Cancer type`)) +
  geom_line(linetype="dashed", aes(color=`Cancer type`), size=0.5)+
  geom_point(color= "yellowgreen", size=2) +
  theme(legend.position = "", 
        legend.background = element_rect(fill="gray95", size=.5, linetype="dotted"),
        panel.background = element_rect(fill = NA, colour = "black",size = 1,linetype = "solid"),
        panel.grid.major.y = element_blank(),
        panel.grid.major.x =  element_line(colour = "grey90",linetype="dotted"),
        panel.grid.minor = element_blank(),
        strip.background = element_blank(),
        strip.text.x = element_text(color="black", size= 6, face="bold.italic", angle=0),
        strip.text.y = element_blank(),
        strip.placement = "outside",
        panel.spacing = unit(0.2, "lines"),
        axis.title = element_text(color="black", size=10, face="plain"),
        axis.text = element_text(color="black", size=10, face="plain"),
        axis.text.x = element_text(color="black", size=10, face="plain", angle = 90, hjust = 1,vjust = 0.3),
        #plot.title = element_text(hjust = 0.5,color="red", size= 10, face="bold.italic"),
        axis.ticks.length = unit(.15, "cm"),
        axis.ticks.x= element_line(size = 1),
        axis.ticks.y= element_line(size = 1)) + 
  scale_y_continuous(expand = c(0.01,0.01)) +
  facet_wrap( `Cancer type` ~., ncol=4,scales = "free_y" )+
  scale_x_log10(breaks = unique(data_6$pvale)) +
  geom_hline(yintercept= 1, colour="blue", linetype="dashed") +
  geom_point(data=data_6[data_6$`Correlation coefficient` == "1",],color="red",size=2)

#output pic
ggsave(filename = paste0(Sys.Date(),"-discribution_each_subject.tif"), plot = last_plot(), 
       device = "tiff", path = dir_path,
       scale = 1, width = 18, height = 16, units = "cm",dpi = 300, 
       limitsize = TRUE, compression = "lzw")
?facet_wrap
## starting httpd help server ... done