r - 如何使用 pROC 的 ggroc 将 AUC 添加到多 ROC 图中

标签 r ggplot2 proc-r-package

我有一个类“roc”(l_rocs)的元素列表,我想用 pROC 包中的 ggroc 绘制它

library("ggplot2")
library("pROC")

#inside a bigger loop
l_rocs[[names[[i]]]] <- roc(predictor=gbm.probs$Yes,
                response=testing$Attrition,
                levels=levels(testing$Attrition))

#loop end

ggroc(l_rocs) +
  labs(color='Sampling Method'))

enter image description here

我现在想为每条曲线添加 AUC。最好的方法就在图例中,但由于给定的元素是一个列表,所以我找不到方法。

有什么建议吗?

最佳答案

这是一个使用修改后的图例标签的解决方案

我使用了一些示例数据,因为没有添加可重现示例的数据。

#library
library(pROC)
library(ggplot2)
library(tidyverse)


# example data
roc.list <- roc(outcome ~ s100b + ndka + wfns, data = aSAH)
#> Setting levels: control = Good, case = Poor
#> Setting direction: controls < cases
#> Setting levels: control = Good, case = Poor
#> Setting direction: controls < cases
#> Setting levels: control = Good, case = Poor
#> Setting direction: controls < cases

# extract auc
roc.list %>% 
  map(~tibble(AUC = .x$auc)) %>% 
  bind_rows(.id = "name") -> data.auc

# generate labels labels
data.auc %>% 
  mutate(label_long=paste0(name," , AUC = ",paste(round(AUC,2))),
         label_AUC=paste0("AUC = ",paste(round(AUC,2)))) -> data.labels

# plot on a single plot with AUC in labels
ggroc(roc.list) +
  scale_color_discrete(labels=data.labels$label_long)

如果你有多条 ROC 曲线,最好绘制一个 facet plot


# plot a facet plot with AUC within plots
ggroc(roc.list) +
  facet_wrap(~name) +
  
  geom_text(data = data.labels,
          aes(0.5, 1, 
              label = paste(label_AUC)),
          hjust = 1) 

reprex package 创建于 2021-09-30 (v2.0.1)

关于r - 如何使用 pROC 的 ggroc 将 AUC 添加到多 ROC 图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66505014/

相关文章:

R - 计算每天每 15 分钟内的总行数

r - 使用 ggplot 在轴末端剪辑标记

r - 调整ggplot中的特定文本标签以避免重叠

r - 不规则数据的时间序列建模

r - 基于两个变量的条件行计数 (R)

使用 multiclass.roc 的 R 多类/多项式分类 ROC(包 ‘pROC’)

r - 如何在 pROC 中绘制具有置信区间的多条 roc 曲线?

r - 如何处理R(pROC程序包)中的多类ROC分析?

r - 如何在 R 中创建相关矩阵?

r - 如何调整饼图上的 ggrepel 标签?