# 【r<-ROC|包】分析与可视化ROC——plotROC、pROC

【r<-绘图|ROC】ROC的计算与绘制这篇文章中我讲了ROC曲线的本质以及如何计算和绘制ROC曲线。注意，我这里谈到的ROC并未曾涉及机器学习模型的拟合与预测，而是指存在一组真实的连续型数值数据设定阈值的不同对响应变量（二分类）的影响（真阳性率、假阳性率）。

• plotROC – Generate ROC Curve Charts for Print and Interactive Use
• pROC – display and analyze ROC curves in R and S+

## plotROC

`plotROC`包较为简单与单一，它就是用来绘制ROC曲线的，包中定义的函数基于`ggplot2`，因此我们可以结合`ggplot2`使用和修改、美化图形结果。

``# 从GitHub上安装 devtools::install_github("hadley/ggplot2") devtools::install_github("sachsmc/plotROC") library(plotROC) # 从CRAN install.packages("plotROC") ``

### 快速使用

`plotROC`提供了Shiny应用，只需要键入

``shiny_plotROC() ``

### 命令行使用

``library(plotROC) set.seed(2529) D.ex <- rbinom(200, size = 1, prob = .5) M1 <- rnorm(200, mean = D.ex, sd = .65) M2 <- rnorm(200, mean = D.ex, sd = 1.5)  test <- data.frame(D = D.ex, D.str = c("Healthy", "Ill")[D.ex + 1],                     M1 = M1, M2 = M2, stringsAsFactors = FALSE)  ``

``basicplot <- ggplot(test, aes(d = D, m = M1)) + geom_roc() basicplot ``

``ggplot(test, aes(d = D, m = M1)) + geom_roc(n.cuts = 0) ``

``ggplot(test, aes(d = D, m = M1)) + geom_roc(n.cuts = 5, labelsize = 5, labelround = 2) ``

``styledplot <- basicplot + style_roc() styledplot ``

``direct_label(basicplot, labels = "Biomarker", nudge_y = -.1) + style_roc() ``

### 绘制多条曲线

`plotROC`提供的函数`melt_roc()`可以将多个变量列变为长格式，方便数据的绘制：

``longtest <- melt_roc(test, "D", c("M1", "M2")) head(longtest) ``
``##     D          M name ## M11 1 1.48117155   M1 ## M12 1 0.61994478   M1 ## M13 0 0.57613345   M1 ## M14 1 0.85433197   M1 ## M15 0 0.05258342   M1 ## M16 1 0.66703989   M1 ``

``ggplot(longtest, aes(d = D, m = M, color = name)) + geom_roc() + style_roc() ``

``plotROC <- function(.data, predict_col, target, group, positive=1, all=TRUE){     if(!(require(tidyverse) & require(plotROC))){         stop("--> tidyverse and plotROC packages are required..")     }           predict_col <- enquo(predict_col)     target <- enquo(target)     group  <- enquo(group)          predictN <- quo_name(predict_col)     groupN   <- quo_name(group)          df <- .data %>% dplyr::select(!! predict_col, !! target, !! group) %>%         mutate(targetN = ifelse(!! target == positive, 1, 0)) %>% as.data.frame()     if (all){         df2 <- df          df2[, groupN] <- "ALL"              df <- rbind(df, df2)     }     p  <- df %>%  ggplot(aes_string(m = predictN,                                      d = "targetN",                                     color = groupN)) + geom_roc(show.legend = TRUE, labels=FALSE)     p <- p + ggpubr::theme_classic2()          ng <- levels(factor(df[, groupN]))     if(length(ng) == 3){         auc <- calc_auc(p)\$AUC         names(auc) <- ng         auc <- base::sort(auc, decreasing = TRUE)         p <- p + annotate("text", x = .75, y = .25,                            label = paste(names(auc)[1], " AUC =", round(auc[1], 3), "/n",                                         names(auc)[2], " AUC =", round(auc[2], 3), "/n",                                         names(auc)[3], " AUC =", round(auc[3], 3), "/n"),                           size = 6)     }          p + xlab("1 - Specificity") + ylab("Sensitivity") +          scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0)) }  ``

``plotROC(longtest, predict_col = M, target = D, group = name, positive = 1) ``