Search code examples
rggplot2proc-r-package

How to add AUC to a multiple ROC graph with pROC's ggroc


I have a list of elements of the class "roc" (l_rocs) which I want to plot with ggroc from the package pROC

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

I now want to add the AUC for each curve. The best would be right inside the legend but I can not find a way to do it as the given element is a list.

Any advice?


Solution

  • Here is a solution using modified legend labels

    I used some example data since no data for a reproducible example was added.

    #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)
    

    If you have multiple ROC curves it might be better to draw a 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) 
    

    Created on 2021-09-30 by the reprex package (v2.0.1)