7

Where am I going wrong here?

I am trying to plot two roc curves on the same plot using ggplot.

I get this error when I run the below code

Error: Don't know how to add o to a plot

ggplot which does not work

ggroc(roc_1) +
  ggroc(roc_2) +
  labs(title = "ROC curve", y = "Sensitivity", x = "Specificity")

base package which works

plot(roc_1, col = 1, lty = 2, main = "ROC")
plot(roc_2, col = 4, lty = 3, add = TRUE)

The dput is too big to post on stackoverflow so here is the structure of one of the ROC calculations.

I am trying to plot two ROC curves similar to below.

List of 15
 $ percent           : logi FALSE
 $ sensitivities     : num [1:26455] 1 1 1 1 1 1 1 1 1 1 ...
 $ specificities     : num [1:26455] 0.00 4.00e-05 8.01e-05 1.20e-04 1.60e-04 ...
 $ thresholds        : num [1:26455] -Inf 0.0017 0.00189 0.00201 0.00214 ...
 $ direction         : chr "<"
 $ cases             : num [1:1540] 0.958 0.919 0.973 0.785 0.933 ...
 $ controls          : num [1:24975] 0.6604 0.026 0.1389 0.0558 0.0594 ...
 $ fun.sesp          :function (thresholds, controls, cases, direction)  
 $ auc               :Classes 'auc', 'numeric'  atomic [1:1] 0.942
  .. ..- attr(*, "partial.auc")= logi FALSE
  .. ..- attr(*, "percent")= logi FALSE
  .. ..- attr(*, "roc")=List of 15
  .. .. ..$ percent           : logi FALSE
  .. .. ..$ sensitivities     : num [1:26455] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. ..$ specificities     : num [1:26455] 0.00 4.00e-05 8.01e-05 1.20e-04 1.60e-04 ...
  .. .. ..$ thresholds        : num [1:26455] -Inf 0.0017 0.00189 0.00201 0.00214 ...
  .. .. ..$ direction         : chr "<"
  .. .. ..$ cases             : num [1:1540] 0.958 0.919 0.973 0.785 0.933 ...
  .. .. ..$ controls          : num [1:24975] 0.6604 0.026 0.1389 0.0558 0.0594 ...
  .. .. ..$ fun.sesp          :function (thresholds, controls, cases, direction)  
  .. .. ..$ auc               :Classes 'auc', 'numeric'  atomic [1:1] 0.942
  .. .. .. .. ..- attr(*, "partial.auc")= logi FALSE
  .. .. .. .. ..- attr(*, "percent")= logi FALSE
  .. .. .. .. ..- attr(*, "roc")=List of 8
  .. .. .. .. .. ..$ percent      : logi FALSE
  .. .. .. .. .. ..$ sensitivities: num [1:26455] 1 1 1 1 1 1 1 1 1 1 ...
  .. .. .. .. .. ..$ specificities: num [1:26455] 0.00 4.00e-05 8.01e-05 1.20e-04 1.60e-04 ...
  .. .. .. .. .. ..$ thresholds   : num [1:26455] -Inf 0.0017 0.00189 0.00201 0.00214 ...
  .. .. .. .. .. ..$ direction    : chr "<"
  .. .. .. .. .. ..$ cases        : num [1:1540] 0.958 0.919 0.973 0.785 0.933 ...
  .. .. .. .. .. ..$ controls     : num [1:24975] 0.6604 0.026 0.1389 0.0558 0.0594 ...
  .. .. .. .. .. ..$ fun.sesp     :function (thresholds, controls, cases, direction)  
  .. .. .. .. .. ..- attr(*, "class")= chr "roc"
  .. .. ..$ call              : language roc.default(response = results$testactual, predictor = results$pred)
  .. .. ..$ original.predictor: num [1:26515] 0.6604 0.026 0.1389 0.0558 0.0594 ...
  .. .. ..$ original.response : int [1:26515] 0 0 0 0 0 0 0 0 0 0 ...
  .. .. ..$ predictor         : num [1:26515] 0.6604 0.026 0.1389 0.0558 0.0594 ...
  .. .. ..$ response          : int [1:26515] 0 0 0 0 0 0 0 0 0 0 ...
  .. .. ..$ levels            : chr [1:2] "0" "1"
  .. .. ..- attr(*, "class")= chr "roc"
 $ call              : language roc.default(response = results$testactual, predictor = results$pred)
 $ original.predictor: num [1:26515] 0.6604 0.026 0.1389 0.0558 0.0594 ...
 $ original.response : int [1:26515] 0 0 0 0 0 0 0 0 0 0 ...
 $ predictor         : num [1:26515] 0.6604 0.026 0.1389 0.0558 0.0594 ...
 $ response          : int [1:26515] 0 0 0 0 0 0 0 0 0 0 ...
 $ levels            : chr [1:2] "0" "1"
 - attr(*, "class")= chr "roc"

Here is how the base package looks, just trying to do it in ggplot

enter image description here

EDIT: Temp solution to the problem.

rocy <- cbind(roc_1$sensitivities, roc_1$specificities, roc_2$sensitivities, roc_2$specificities)
rocy <- as.data.frame(rocy)
ggplot(rocy) + 
  geom_line(aes(y = V1, x = V2)) +
  geom_line(aes(y = V3, x = V4))

EDIT: Solution

library(pROC)
rocobj1 <- roc(df$actualoutcome1, data$prediction1)
rocobj2 <- roc(df$actualoutcome1, data$prediction2)
ggroc(list(call_roc_name_1 = rocobj1, call_roc_name_2 = rocobj2))
5
  • 1
    why don't you simply use the sensitivities and specificities of multiple your two ROCs to plot them on your own? cran.r-project.org/web/packages/ggROC/ggROC.pdf here you can find the code of the function ggroc and it is not a complicate function. I think you can customize it easily
    – Seymour
    Commented Apr 10, 2018 at 21:20
  • 1
    Look at the manual page for ggroc: it says this function returns a ggplot object. You can't add two ggplot objects to each other---you build the object and then add layers to it
    – camille
    Commented Apr 10, 2018 at 21:20
  • Thanks, I will take a look through the ggroc manual again. What I did as a backup was to extract the elements of the roc output and save it as a data.frame. It seemed to do the trick, but was thinking that there was a more elegant solution. rocy <- cbind(roc_1$sensitivities, roc_1$specificities, roc_2$sensitivities, roc_2$specificities) rocy <- as.data.frame(rocy) ggplot(rocy) + geom_line(aes(y = V1, x = V2)) + geom_line(aes(y = V3, x = V4))
    – user113156
    Commented Apr 10, 2018 at 21:33
  • 1
    Google brought me to a blog post by the developer (I believe) of the pROC package that you're using, explaining how to do this: xavier.robin.name/blog
    – camille
    Commented Apr 11, 2018 at 2:23
  • Thanks followed the link and works perfect, library(pROC) rocobj <- roc(df$actualoutcome1, data$prediction1) rocobj2 <- roc(df$actualoutcome1, data$prediction2) ggroc(list(call_roc_name_1 = rocobj1, call_roc_name_2 = rocobj2))
    – user113156
    Commented Apr 11, 2018 at 21:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.