rm(list = ls())
###############################input data 
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\data1\\"
files_list <- dir(dir_path, pattern = "*.",full.names = T, recursive = TRUE)
#files_list
#################################################data_1
data_1 <- read.csv(grep("compounds_list.csv",files_list,value = T),header = T,stringsAsFactors = F)
#dim(data_1) #[1] 127   6
data_1 <- data_1[data_1$label == "Inhibitor", ]
#data_1 <- data_1[data_1$AC50..uM < 2, ]
data_1 <- data_1[order(data_1$AC50..uM),][1:10,]
#dim(data_1) #[1] 14  6
####################################################
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.1.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library('ggsci')
data_1$Sample.ID <- gsub("-.*", "", data_1$Sample.ID)
data_1$Sample.ID <- factor(data_1$Sample.ID, levels = data_1[order(data_1$AC50..uM),]$Sample.ID)
p1 <- ggplot(data_1, aes(y=Sample.ID, x = AC50..uM, color = Sample.ID)) + 
  #geom_bar(position=position_dodge(0.9), stat="identity",
  #colour="red", # Use black outlines
  #         fill=NA, 
  #        size=0.5) +      # Thinner lines
  geom_point(size= 2,
             position = "identity") + 
  geom_errorbar(aes(xmin=AC50..uM - AC50..uM_sd, xmax=AC50..uM + AC50..uM_sd, color = Sample.ID), 
                width=.3,position = "identity")+
  ylab("") +
  xlab(expression(paste(IC[50], " (μM)"))) +
  ggtitle("") +
  theme(legend.position = "none",
        #legend.direction = "vertical",
        #legend.spacing.x = unit(1, 'cm'),
        #legend.spacing.y = unit(0.1, 'cm'),
        #legend.title.align = 0.1,
        legend.key.size = unit(.9, "cm"),
        legend.text = element_text(colour="black", size=12, 
                                   face="plain"),
        # legend.title = element_text(colour="red", size=10, 
        #                            face="bold"),
        #legend.background = element_rect(fill="lightblue",
        #                                size=2, linetype="solid", 
        #                                colour ="blue"),
        #legend.key.height=unit(1.2,"line"),
        #legend.key.width=unit(1.2,"line"),
        #legend.margin=margin(5,5,5,5),
        #legend.justification = c(0,1),
        #legend.box.margin=margin(0,0,0,0),
        legend.key = element_rect(colour = NA, fill = NA),
        panel.background = element_blank(),
        panel.border = element_rect(colour = NA, fill= NA, size=1),
        panel.grid = element_blank(),
        axis.text.x   = element_text(size= 12, color = "black",family = "sans",hjust = 0.5,vjust = -1,angle = 0),
        axis.text.y   = element_text(size= 12, color = "black",family = "sans",vjust = 0.5,hjust = 0.5,angle = 0),
        axis.title.x  = element_text(size=12, color = "black",family = "sans",margin=margin(8,0,0,0)),
        axis.ticks =  element_line(size= 0.5),
        axis.ticks.length = unit(3, "pt"),
        #axis.title=element_text(size=14,face="bold"),
        axis.line = element_line(size = 0.5, colour = "black"),aspect.ratio=1)
p1

p2 <- p1 + scale_color_manual(values=c(pal_aaas("default")(10), pal_aaas("default")(2))) +
  scale_x_log10()  + coord_cartesian(clip="off") +
  annotation_logticks(sides = "b", 
                   short=unit(-0.1, "cm"), 
                   mid=unit(-0.3, "cm"), long=unit(-0.3,"cm")) 
p2

ggsave(paste0(Sys.Date(),"-ac50.tiff"), 
       plot = last_plot(), device = NULL, path = dir_path,
       scale = 1, width = 12, height = 12, units ="cm",dpi = 300, 
       limitsize = TRUE,compression = "lzw")