3

我有一个“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'))

在此处输入图像描述

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

有什么建议吗?

4

1 回答 1

1

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

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

#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 曲线,最好绘制一个平面图


# 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 包于 2021-09-30 创建(v2.0.1)

于 2021-09-30T14:41:32.117 回答