rm(list = ls())
###############################input data_1 
dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
dir_path_name <- dir(dir_path,pattern = ".*.csv",full.names = T)
dir_path_name
## [1] "C:\\Users\\liyix\\OneDrive\\Desktop\\data_1.csv"
## [2] "C:\\Users\\liyix\\OneDrive\\Desktop\\data_2.csv"
###############################1.1 bath input data 
data_1 <- read.csv(grep("data_1.csv",dir_path_name,value = T),header = T,stringsAsFactors = F)
data_2 <- read.csv(grep("data_2.csv",dir_path_name,value = T),header = T,stringsAsFactors = F)
dim(data_1);dim(data_2) #[1] 1389    3 #[1] 804   3
## [1] 1389    3
## [1] 804   3
head(data_1)
##   probability_0 probability_1 Mapping.ID
## 1     0.5189319   0.481068111          0
## 2     0.9831460   0.016854033          0
## 3     0.9981004   0.001899579          0
## 4     0.9932042   0.006795812          0
## 5     0.9944267   0.005573279          0
## 6     0.9434477   0.056552347          0
head(data_2)
##   probability_0 probability_1 Mapping.ID
## 1         0.880         0.120          0
## 2         0.550         0.450          1
## 3         1.000         0.000          0
## 4         0.940         0.060          0
## 5         0.518         0.482          0
## 6         0.950         0.050          0
##############################################calculate
library(ggplot2)
library(pROC)
## Type 'citation("pROC")' for a citation.
## 
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
data_11 <- roc(data_1$Mapping.ID,data_1$probability_1)
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
data_21 <- roc(data_2$Mapping.ID,data_2$probability_1)
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
rocobj <- list(Data_1 = data_11, Data_2 = data_21)
##############################################drawing plot
ggroc(rocobj,aes=c("linetype", "color"),linetype = 1,size = 0.6,alpha=1)+
labs(x = "1-Specificity", y = "Sensitivity") + 
  geom_abline(intercept = 1, slope = 1, color='grey',size = 0.5,linetype = "dashed") + 
  theme(panel.background = element_rect(fill = "white",
                                        colour = "white",
                                        size = 0, linetype = "solid"),
    panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.position = c(0.8, 0.15),
        legend.box = "vertical",
        legend.key = element_rect(colour = NA, fill = NA),
        legend.title = element_text(colour="black",size=16,face="plain",family = "sans"),
        axis.line =element_line(colour = "black", 
                               size = 0.1, linetype = "solid"),
        legend.background = element_rect(fill="white",
                                         size=0.5, linetype="solid", 
                                         colour ="white"),
        legend.text=element_text(size=13,family = "sans"),
        #axis.text.x = element_blank(),
        axis.text.x = element_text(colour="black",size=16,face="plain",family = "sans"),
        axis.text.y = element_text(colour="black",size=16,face="plain",family = "sans"),
        axis.title.x = element_text(colour="black",size=16,face="plain",family = "sans"),
        axis.title.y = element_text(colour="black",size=16,face="plain",family = "sans")) +
  scale_y_continuous(expand = c(0.01, 0.001),breaks= seq(from = 0 , to = 1 ,by = 0.2))+
  scale_x_reverse(expand = c(0.01, 0.001),breaks= seq(from = 0 , to = 1 ,by = 0.2),limits = c(1,-0.01),
                  labels = c("1.0","0.8","0.6","0.4","0.2","0.0")) +
 scale_color_manual(name = "",values=c("#3b58a7","#90278e"),
                    labels = c("From Data_1", "From Data_2"))
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

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

?scale_x_reverse
## starting httpd help server ...
##  done