在 ggplot 中添加 abline 并调整标签的问题

Issue with adding abline in ggplot and adjusting labels

我正在使用 R 中的 ggplot 构建和自定义 ROC 曲线。除了 2 个挑战,我几乎完成了。

  1. 由于某种原因我添加的 abline 超出了 0 和 1。如何限制它?该线应从原点开始并转到 (1,1)

  2. 如何将小数点标签显示为百分比?

到目前为止我写的代码:

ggplot(roc, aes(x=fpr, ymin=0, ymax=tpr)) +
geom_ribbon(alpha=0.2) +
geom_line(aes(y=tpr)) +
ggtitle(paste0("ROC Curve, AUC=",auc)) +
xlab("Cumulative % Goods")+
ylab("Cumulative % Bads")+
geom_abline(intercept=0, slope = 1, color="red", linetype="dashed")+
theme_classic()

我也尝试添加 scale_y_continuous(labels='percent') 但它给我一个错误 "breaks and labels must have the same length"

我找到了解决方案 - 我使用了 "scales" 库并向图中添加了 2 个元素。完整代码如下:

ggplot(roc, aes(x=fpr, ymin=0, ymax=tpr)) +
    geom_ribbon(alpha=0.1) +
    geom_line(aes(y=tpr)) +
    ggtitle(paste0("ROC Curve, AUC=",auc)) +
    xlab("Cumulative % Goods")+
    ylab("Cumulative % Bads")+
    geom_abline(intercept=0, slope = 1, color="red", linetype="dashed")+
    theme_classic()+
    scale_y_continuous(labels=percent, expand=c(0,0))+
    scale_x_continuous(labels=percent, expand=c(0,0))