rm(list = ls())
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- dir(dir_path,pattern = "*.xlsx",full.names = T)
dir_path_name
## [1] "C:\\Users\\liyix\\OneDrive\\Desktop\\data.xlsx"
library(openxlsx)
getSheetNames(grep("data.xlsx",dir_path_name,value = T))[5]
## [1] "AUCs"
data_1 <- read.xlsx(grep("data.xlsx",dir_path_name,value = T),sheet = 5)
##View(Guide)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## √ ggplot2 3.3.3     √ purrr   0.3.4
## √ tibble  3.0.6     √ dplyr   1.0.4
## √ tidyr   1.1.2     √ stringr 1.4.0
## √ readr   1.4.0     √ forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
data_1
##   Test.ROC    data_1    data_2
## 1        A 0.88±0.01 0.89±0.03
## 2        B 0.85±0.01 0.85±0.02
## 3        C 0.85±0.01 0.87±0.02
## 4        D 0.84±0.02 0.85±0.02
data_2 <- gather(data_1, key = key,value = value, -1)
data_2 
##   Test.ROC    key     value
## 1        A data_1 0.88±0.01
## 2        B data_1 0.85±0.01
## 3        C data_1 0.85±0.01
## 4        D data_1 0.84±0.02
## 5        A data_2 0.89±0.03
## 6        B data_2 0.85±0.02
## 7        C data_2 0.87±0.02
## 8        D data_2 0.85±0.02
data_2$sd <- data_2$value
data_2$value <- gsub("±.*", "", data_2$value)
data_2$sd <- gsub(".*±", "", data_2$sd)
data_2 
##   Test.ROC    key value   sd
## 1        A data_1  0.88 0.01
## 2        B data_1  0.85 0.01
## 3        C data_1  0.85 0.01
## 4        D data_1  0.84 0.02
## 5        A data_2  0.89 0.03
## 6        B data_2  0.85 0.02
## 7        C data_2  0.87 0.02
## 8        D data_2  0.85 0.02
str(data_2)
## 'data.frame':    8 obs. of  4 variables:
##  $ Test.ROC: chr  "A" "B" "C" "D" ...
##  $ key     : chr  "data_1" "data_1" "data_1" "data_1" ...
##  $ value   : chr  "0.88" "0.85" "0.85" "0.84" ...
##  $ sd      : chr  "0.01" "0.01" "0.01" "0.02" ...
data_2$value <- as.numeric(data_2$value)
data_2$sd <- as.numeric(data_2$sd)
colnames(data_2)
## [1] "Test.ROC" "key"      "value"    "sd"
###############################################plot
draw_key_polygon3 <- function(data, params, size) {
  lwd <- min(data$size, min(size) / 4)
  
  grid::rectGrob(
    width = grid::unit(0.8, "npc"),
    height = grid::unit(0.8, "npc"),
    gp = grid::gpar(
      col = data$colour,
      fill = alpha(data$fill, data$alpha),
      lty = data$linetype,
      lwd = lwd * .pt,
      linejoin = "mitre"
    ))
}

# register new key drawing function, 
# the effect is global & persistent throughout the R session
GeomBar$draw_key = draw_key_polygon3
ggplot(data_2, aes(x=key, y=value, fill=Test.ROC)) + 
  geom_bar(position=position_dodge(.9), stat="identity",
           colour="black", # Use black outlines
           size=.3) +      # Thinner lines
  geom_errorbar(aes(ymin = value - sd, ymax = value + sd),
                size=.3,    # Thinner lines
                width=.2,
                position=position_dodge(0.9)) +
  xlab("") +
  ylab("AUC-ROC") +
  ggtitle("") +
  scale_fill_manual(name="Model", # Legend label, use darker colors
                    values=c("#3D79F3FF", "#E6352FFF", "#F9B90AFF", "#34A74BFF")) +
  #"#E6352FFF",  "#3D79F3FF", "purple", "#34A74BFF"
  scale_y_continuous(expand = c(0,0),breaks = seq(0.5,0.95,.1)) +
  coord_cartesian(ylim = c(0.5,0.95)) +
  theme(legend.position = "right",
        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(.5, "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(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),
        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 = 0.5),
        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"))

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