我try 使用以下代码绘制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).

对如何解决这个问题有什么 idea 吗?我不知道为什么它不起作用.

推荐答案

一种可能的解决方案是在ggroc()中指定legacy.axes = TRUE,然后更改geom_segment()以适应,例如

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)

创建于2023-03-27,共reprex v2.0.2

R相关问答推荐

根据列表中项目的名称多次合并数据框和列表

根据R中的另一个日期从多列中 Select 最近的日期和相应的结果

判断字符串中数字的连续性

将数据集中的值增加到当前包含的最大值

如何使用STAT_SUMMARY向ggplot2中的密度图添加垂直线

如何根据嵌套元素的名称高效而优雅地确定它属于哪个列表?

使用外部文件分配变量名及其值

如何在R中描绘#符号?

使用rest从header(h2,h3,table)提取分层信息

绘制采样开始和采样结束之间的事件

如何根据R中其他变量的类别汇总值?

计算直线上点到参考点的总距离

KM估计的差异:SvyKm与带权重的调查

在R中使用列表(作为tibble列)进行向量化?

使用shiny 中的所选要素行下拉菜单

根据r中每行中的日期序列,使用列名序列创建新列

如何预测原始数据集并将值添加到原始数据集中

策略表单连接两个非常大的箭头数据集,而不会 destruct 内存使用

将边列表转换为路径长度列表

我有2011-2022年的年度数据.如何计算最低年份和最高年份之间的差额?