rm(list = ls())
###############################input data 
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- list.files(pattern = ".*csv",dir_path,full.names = T, recursive = F)
dir_path_name
## [1] "C:\\Users\\liyix\\OneDrive\\Desktop\\2021-08-13-merge_FOUR.csv"
data_1 <- read.csv(grep("2021-08-13-merge_FOUR.csv",dir_path_name,value = T),header = T,stringsAsFactors = F)
###############################input data 
dim(data_1)
## [1] 20  8
data_1 <- data_1[1:15,]
################################################
library(ggplot2)
library(RColorBrewer)

df1 <- lapply(split(data_1, data_1$Source),
              function(x) x[which.max(x$AUC_ROC),])
df_2 <- do.call('rbind', df1)
df_2$label <- "*"

df_3 <- merge(data_1, df_2, by = colnames(data_1), all.x = T)
data_1 <- df_3

data_1$Source <- factor(data_1$Source, levels = unique(data_1$Source)[c(2,1,3,4)],labels = c("A","B","C"))
p1 <- ggplot(data_1, aes(x=Source, y= AUC_ROC, fill= machine.learning.algorithm)) + 
  geom_bar(position=position_dodge(.9), stat="identity",
           colour="black", # Use black outlines
           size=.3) +      # Thinner lines
  geom_errorbar(aes(ymin = AUC_ROC - SD_AUC, ymax = AUC_ROC + SD_AUC),
                size=.3,    # Thinner lines
                width=.2,
                position=position_dodge(0.9)) +
  xlab("") +
  ylab("value") +
  ggtitle("") +
  scale_fill_manual(name="Model", # Legend label, use darker colors
                    values= brewer.pal(n = 5, name = "Pastel1")) +
  scale_y_continuous(expand = c(0,0),breaks = seq(0.5,0.9,0.1)) +
  coord_cartesian(ylim = c(0.7,0.92)) 

p1 

p3 <- p1 + theme(legend.position = "top",
                 #legend.direction = "vertical",
                 legend.spacing.x = unit(0.1, 'cm'),
                 #legend.spacing.y = unit(0.2, 'cm'),
                 legend.title.align = 0.1,
                 legend.key.size = unit(.3, "cm"),
                 legend.text = element_text(colour="black", size=10, 
                                            face="plain"),
                 legend.title = element_text(colour="black", size=10, 
                                             face="plain"),
                 legend.background = element_blank(),
                 legend.key = element_rect(colour = NA, fill = NA),
                 #legend.key.height=unit(0.5,"line"),
                 #legend.key.width=unit(0.7,"line"),
                 #legend.margin=margin(5,5,5,5),
                 legend.justification = c(0.5, 0.7),
                 legend.box.margin=margin(0,0,0,0),
                 panel.background = element_blank(),
                 panel.border = element_rect(colour = NA, fill=NA, size=1),
                 axis.line = element_line(size = 0.5, color = "black"),
                 panel.grid = element_blank(),
                 axis.text.x   = element_text(size= 12, color = "black",family = "sans",hjust = 1, angle = 45),
                 axis.text.y   = element_text(size= 12, color = "black",family = "sans",vjust = 0.5,hjust = 0.5),
                 axis.title  = element_text(size=12, color = "black",family = "sans"),
                 axis.ticks =  element_line(size= 0.5),
                 axis.ticks.length = unit(3, "pt")) 
p3

p4 <- p3 +  geom_text(data = data_1, size = 6, aes(x=Source, label = label,y = 0.88), 
                      colour = "darkblue", family = "sans",position=position_dodge(width=0.9), vjust=-0.25)
p4
## Warning: Removed 12 rows containing missing values (geom_text).

###################################################
ggsave(paste0(Sys.Date(),"-model_3.tiff"), 
       plot = last_plot(), device = NULL, 
       path = dir_path,
       scale = 1, width = 10, height = 11, units ="cm",dpi = 300, 
       limitsize = TRUE,compression = "lzw")
## Warning: Removed 12 rows containing missing values (geom_text).