I could create a ROC
curve for my Statistical analysis
library(pROC)
plot(roc(aSAH$outcome, aSAH$s100b,
levels=c("Good", "Poor")))
However I would like plot the same using ggplot
for additional styling, so I'm looking for some way on extracting the x-y
values so that I could use them in ggplot
.
I would appreciate for any pointer.
The output of roc(...)
is a list-like structure of class "roc"
which has members called "specificities"
and "sensitivities"
. You could simply extract and use these for your plot:
proc_obj <- roc(aSAH$outcome, aSAH$s100b, levels=c("Good", "Poor"))
plot_df <- data.frame(Specificity = proc_obj$specificities,
Sensitivity = proc_obj$sensitivities)
However, pROC
contains the function coords
specifically for the purpose of getting the sensitivities and specificities into a data frame, so it saves a few keystrokes to use that instead.
library(pROC)
library(ggplot2)
roc(aSAH$outcome, aSAH$s100b, levels=c("Good", "Poor")) |>
coords() |>
ggplot(aes(specificity, sensitivity)) +
geom_step() +
geom_abline(intercept = 1, slope = -1, colour = "gray") +
coord_trans(x = "reverse", expand = FALSE, clip = "off") +
theme_minimal(16) +
theme(aspect.ratio = 1)
This is assuming that for some reason you don't want to create your ggplot object with ggroc
, which is another function in pROC
specifically for creating ggplot objects from ROC curves:
ggroc(roc(aSAH$outcome, aSAH$s100b, levels=c("Good", "Poor"))) +
theme_minimal(16) +
theme(aspect.ratio = 1) +
geom_abline(intercept = 1, slope = 1, colour = "gray")