带有 ROC 曲线的 ggplot 中的 scale_x_reverse 问题

问题描述 投票:0回答:1

我尝试使用以下代码绘制我的 ROC 曲线:

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)

但是,当我收到此错误消息时,“scale_x_reverse”存在问题:“x 的比例已经存在。 为 x 添加另一个比例,它将取代现有的比例。”。

我希望 X 轴从 0 到 1(因此将当前的 1 恢复为 0)。

关于如何解决问题的任何想法?我不知道为什么它不起作用。

r database dataframe ggplot2 roc
1个回答
1
投票

我们可以使用

scale_x_reverse()
函数的
limits
参数将 x 轴从 0 限制到 1,而不是
scale_x_continuous()

library(titanic)
library(pROC)
library(ggplot2)

p <- ggroc(r) +
  scale_x_continuous(limits = c(0,1)) +
  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)

© www.soinside.com 2019 - 2024. All rights reserved.