rm(list = ls())
library(ggplot2)
###############################input data 
dir_path <- "C:/Users/liyix/OneDrive/Desktop/"
dir_path_name <- dir(dir_path,pattern = "data.xlsx")
dir_path_name
## [1] "data.xlsx"
###############################bath input data 
library(openxlsx)
object_file_list <- list()
for (i in 1:1) {
  dir_path_file <- paste0(dir_path,dir_path_name) 
  object_file_list[[i]] <- read.xlsx(dir_path_file,sheet = i)
  names(object_file_list)[i] <- getSheetNames(dir_path_file)[i]
}
##
#colnames(object_file_list[[i]])
#dim(object_file_list[[i]])
#names(object_file_list)
#######################################papare data
for (j in 1:1) {
  df <-  object_file_list[[j]][,c("Fold.change","p-value")]
  dim(df) #[1] 5256    2
  head(df)
  colnames(df) <- c("FC","P.Value")
  ################################################FC < 0.5 & P.Value < 0.05
  ###define Green----downregulation 
  df.G <- subset(df, FC < 0.5 & P.Value < 0.05) #define Green
  df.G <- cbind(df.G, rep("Downregulation", nrow(df.G)))
  colnames(df.G)[3] <- "Color"
  head(df.G)
  df.G$FC <- log2(abs(df.G$FC))
  #View(df.G)
  dim(df.G) #[1] 412   2
  #################################################define Black
  df.B <- subset(df, FC >= 0.5 & FC <= 2 | P.Value >= 0.05) #define Black
  df.B <- cbind(df.B, rep("Not change", nrow(df.B)))
  colnames(df.B)[3] <- "Color"
  #less than zero
  head(df.B)
  df.B$FC <- log2(abs(df.B$FC))
  dim(df.B) #[1] 4805    3
  ##################################################define Red
  df.R <- subset(df, FC > 2 & P.Value < 0.05) #define Red
  df.R <- cbind(df.R, rep("Upregulation", nrow(df.R)))
  colnames(df.R)[3] <- "Color"
  head(df.R)
  #View(df.R)
  df.R$FC <- log2(abs(df.R$FC))
  dim(df.R)#[1] 39  3
  ##########################################MERGE
  df.t <- rbind(df.G, df.B,df.R)
  #View(df.t)
  str(df.t)
  df.t[,3] <- as.factor(df.t[,3])
  dim(df.t) #[1] 5256    3
  ##Construct the plot object
}
## 'data.frame':    5256 obs. of  3 variables:
##  $ FC     : num  -1.6 -2.53 -2.71 -1.08 -1.49 ...
##  $ P.Value: num  0.00514 0.00992 0.01766 0.02248 0.02521 ...
##  $ Color  : chr  "Downregulation" "Downregulation" "Downregulation" "Downregulation" ...
names(object_file_list)[[j]]
## [1] "B-A"
##########################################plot
dim(df)
## [1] 5256    2
head(df)
##          FC     P.Value
## 1 0.3294268 0.005140125
## 2 0.1727447 0.009919530
## 3 0.1528525 0.017657911
## 4 0.4715006 0.022482074
## 5 0.3555312 0.025214794
## 6 0.4172340 0.027089710
ggplot(data = df.t, aes(x = FC, y = -log10(P.Value), color= Color )) +
  geom_point(alpha = 0.5, size = 1.5,shape = 21) +
  theme(text=element_text(family="sans"),
        plot.title = element_text(hjust = 0.5),
        panel.border = element_rect(linetype = "solid", fill = NA,colour = "black", size = 0.5),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.position = "none",
        axis.line = element_line(colour = "black",size = 0.5),
        axis.text.x = element_text(colour="black",size=13,face="plain"),
        axis.text.y = element_text(colour="black",size=13,face="plain"),
        axis.title.x = element_text(colour="black",size=15,face="plain"),
        axis.title.y = element_text(colour="black",size=15,face="plain")) +
  scale_color_manual(values = c("#FF0000", "black", "#FF0000")) +
  labs(title = "",x="log2 fold change", y= "-log10 p-value") +
  geom_hline(yintercept= -log10(0.05), linetype="dashed", 
             color = "grey", size= 0.5) +
  geom_vline(xintercept = log2(0.67), linetype="dashed", 
             color = "grey", size= 0.5) +
  geom_vline(xintercept = log2(1.5), linetype="dashed", 
             color = "grey", size= 0.5) +
  scale_y_continuous(expand = c(0.01,0.01))

################output
ggsave(paste0(names(object_file_list)[[1]],".tiff"), plot = last_plot(), device = "tiff", path = dir_path,
       scale = 1, width = 15, height = 18, units ="cm",dpi = 300, limitsize = TRUE)

#axis.line.x = element_line(color="black", size = 1),
#axis.line.y = element_line(color="black", size = 1)