3

I have created a ROC plot with multiple ROC-curves using ggroc based on pROC. How can I insert a line of no discrimination?

I would like to have a line of no discrimination from 0,0 to 1,1 in my plot, so that I can better visually evaluate my ROC-curves.

I have tried using the plot() function on my ggplot object, and I have tried using + geom_abline(), and the lines() function without any luck.

library(pROC)

#Creating curves and labeling)
ROC_curves <- ggroc(list(log=ROC_log, tree=ROC_tree, xgbt=ROC_xgbt), legacy.axes=TRUE)

ROC_curves2 <- ROC_curves + xlab("FPR") + ylab("TPR")
#but this part doesn't Work: 
+ qplot(1,1) + geom_abline(intercept=0, slope=1)

I have also tried doing: plot(ROC_curves2, identity=TRUE)

I would like a line of no discrimination going from 0,0 to 1,1 in my plot.

When adding qplot(1,1) + geom_abline(), I get "Error: Don't know how to add o to a plot". When using plot() a plot is returned, but still with no line.

Calimo
  • 7,510
  • 4
  • 39
  • 61
rica
  • 67
  • 1
  • 9

2 Answers2

3

The ROC_curves already returns a ggplot plot. Adding a new plot to it with qplot is not possible nor necessary, just add geom_abline directly:

ROC_curves + xlab("FPR") + ylab("TPR") + 
    geom_abline(intercept = 0, slope = 1,
                color = "darkgrey", linetype = "dashed")

The abline extends beyond the limits of the ROC curve. To avoid that you can use geom_segment instead:

ROC_curves + xlab("FPR") + ylab("TPR") + 
    geom_segment(aes(x = 0, xend = 1, y = 0, yend = 1),
                 color="darkgrey", linetype="dashed")

Also note that if you weren't using legacy.axes=TRUE you would need to have intercept = 1 so that the line crosses the 0 line on the top right.

... + geom_segment(aes(x = 0, xend = 1, y = 0, yend = 1)) # legacy.axes = TRUE
... + geom_segment(aes(x = 1, xend = 0, y = 0, yend = 1)) # legacy.axes = FALSE
Calimo
  • 7,510
  • 4
  • 39
  • 61
1

@Calimo's solution didn't work for me but I think that is due to the size of my dataset so the graph won't render. Found a gitlab issue (https://github.com/tidyverse/ggplot2/issues/4190) about how annotate is much faster than geom_segment. I'm using the following:

+ annotate("segment",x = 1, xend = 0, y = 0, yend = 1, color="red", linetype="dashed")

rkbarney
  • 38
  • 4