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

时间:2016-01-15 09:29:35

标签: r ggplot2 roc

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

  1. 由于某种原因,我添加的abline延伸到0和1之外。如何限制?该行应从原点开始,然后转到(1,1)

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

  3. 到目前为止我编写的代码:

    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')但是它会给我一个错误“中断并且标签必须具有相同的长度”

    How it looks like now

1 个答案:

答案 0 :(得分:0)

我找到了解决方案 - 我使用了“scale”库并在图中添加了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))