rm(list = ls())
###############################input data_1 
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\data\\"
dir_path_name <- dir(dir_path,pattern = ".*.csv",full.names = T)
#dir_path_name
###############################merge data 
data_1 <- read.csv(grep("data_1.csv",dir_path_name,value = T),header = T,stringsAsFactors = F)
#dim(data_1) #[1] 882   7
#head(data_1,2)
pro <- data.frame(table(data_1$cancer_type))
pro$Var1 <- LETTERS[1:nrow(pro)]
#dim(pro) #[1] 23022     5
#View(pro)
#head(pro)
dat <- pro
colnames(dat) <- c("source", "unique_drug")
#library(stringr)
#dat$source <- str_to_title(dat$source) 
#dat$source[dat$source == "Aml"] <- "AML"
######################################################
#head(dat)
dat$source <- factor(dat$source, levels = unique(dat[order(dat$unique_drug, decreasing = T),]$source))
#####################################################
library(ggplot2)
library(tidyr)
library(dplyr)
## 
## 载入程辑包:'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
dat_1 <- dat
dat_1$label <- "n"
dat_1$unique_drug <- dat_1$unique_drug/2
dat_2 <- dat_1
dat_2$label <- "m"
dat_2$unique_drug <- -1*dat_2$unique_drug
p1 <- ggplot(dat_1, aes(x=unique_drug, y= source, fill = unique_drug)) + 
  geom_bar(position=position_dodge(.9), stat="identity",
           colour="black", #fill =  "#B3CDE3", # Use black outlines
           size=.3) +      # Thinner lines
  #geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
  #             size=.3,    # Thinner lines
  #            width=.2,
  #           position=position_dodge(0.9)) +
  xlab("") +
  ylab("") +
  ggtitle("") + 
  scale_x_continuous(expand = c(0,0), limits = c(0, max(dat_1$unique_drug) +5)) +
  scale_fill_gradient2(low = "white", high = "skyblue", name = "Number of drugs") + 
  theme(panel.background = element_blank(), axis.ticks = element_blank(),
        axis.text = element_blank())
p1 

p2 <-  ggplot(dat_2, aes(x=unique_drug, y= source, fill = unique_drug)) + 
  geom_bar(position=position_dodge(.9), stat="identity",
           colour="black", #fill =  "#B3CDE3", # Use black outlines
           size=.3) +      # Thinner lines
  xlab("") +
  ylab("") +
  ggtitle("") + 
  scale_x_continuous(expand = c(0,0), limits = c(min(dat_2$unique_drug) - 5,0)) +
  scale_fill_gradient2(low = "skyblue", high = "white") + 
  theme(panel.background = element_blank(), 
        axis.ticks = element_blank(),
        axis.text.x = element_blank(),
         legend.position = "none")
p2

###################################
library(patchwork)
(p2 +  theme(plot.margin = unit(c(0,0,0,0), "cm"),
             plot.background = element_rect(fill="white")))+
  (p1 +  theme(plot.margin = unit(c(0,0,0,0), "cm"),
               axis.ticks.length.y = unit(0, "mm"),
               axis.ticks.x = element_blank(),
               axis.title = element_blank(),
               panel.background=element_rect(fill="white"),
                 plot.background = element_rect(fill="white"))) +
  plot_layout(widths = c(1,1), guides = "auto") +
  theme(legend.position=c(0.78,0.85))

###################################
ggsave(filename = paste0(Sys.Date(),"-barplot.tif"), plot = last_plot(), 
       device = "tiff", path = dir_path,
       scale = 1, width = 16, height = 16, units = "cm",dpi = 300, limitsize = TRUE, compression = "lzw")