I tried plotting my ROC curve using the following code:
library(titanic)
library(pROC)
library(ggplot2)
r <- roc(Survived ~ Fare, data = titanic_train)
#AUC text
auc <- auc(r)
ci <- ci.auc(r)
ci_l <- round(ci[1], 2)
ci_u <- round(ci[3], 2)
legend_text <- paste0("AUC = ", round(auc, 2), " (95% CI = ", ci_l, " - ", ci_u, ")")
#Plot
p <- ggroc(r) +
scale_x_reverse() +
labs(
title="ROC",
y = "Sensitivity",
x = "1 - Specificity"
) +
geom_segment(aes(x=1, xend=0, y=0, yend=1), color="grey", linetype="dashed") +
annotate("text", x = 0.3, y = 0.05, label = legend_text)
print(p)
However, there is problem with "scale_x_reverse" as I get this error message: "Scale for x is already present. Adding another scale for x, which will replace the existing scale.".
I want the X axis to be from 0 to 1 (thus revert the current 1 to 0).
Any ideas on how to solve the problem? I don't know why it doesn't work.
One potential solution is to specify legacy.axes = TRUE
in ggroc()
, then change geom_segment()
to suit, e.g.
library(titanic)
library(pROC)
#> Type 'citation("pROC")' for a citation.
#>
#> Attaching package: 'pROC'
#> The following objects are masked from 'package:stats':
#>
#> cov, smooth, var
library(ggplot2)
r <- roc(Survived ~ Fare, data = titanic_train)
#> Setting levels: control = 0, case = 1
#> Setting direction: controls < cases
auc <- auc(r)
ci <- ci.auc(r)
ci_l <- round(ci[1], 2)
ci_u <- round(ci[3], 2)
legend_text <- paste0("AUC = ", round(auc, 2), " (95% CI = ", ci_l, " - ", ci_u, ")")
p <- ggroc(r, legacy.axes = TRUE) +
labs(
title="ROC",
y = "Sensitivity",
x = "1 - Specificity"
) +
geom_segment(aes(x=0, xend=1, y=0, yend=1), color="grey", linetype="dashed") +
annotate("text", x = 0.3, y = 0.05, label = legend_text)
print(p)
Created on 2023-03-27 with reprex v2.0.2