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
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))
sensitivities
andspecificities
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 functionggroc
and it is not a complicate function. I think you can customize it easilyggroc
: it says this function returns aggplot
object. You can't add twoggplot
objects to each other---you build the object and then add layers to itggroc
manual again. What I did as a backup was to extract the elements of theroc
output and save it as adata.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))
pROC
package that you're using, explaining how to do this: xavier.robin.name/bloglibrary(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))