R语言绘图:ggplot2绘制ROC

使用ggplot2包绘制ROC曲线

rocplot<- function(pred, truth, ...){
    predob<- prediction(pred, truth)
    #打印AUc
    perf.auc<- performance(predob, measure = 'auc', x.measure = 'cutoff')
    #
    perf<- performance(predob, 'tpr','fpr')
    df<- data.frame(x = attributes(perf)$x.values[[1]],y = attributes(perf)$y.values[[1]])  
    p    <- ggplot(data = df)
    p + geom_line(aes(x,y),colour = "yellowgreen",size = 1) + 
        geom_ribbon(aes(x,ymin = 0,ymax = y),fill = alpha("yellowgreen",0.5)) +
        labs(title = paste("ROC Curve & AUC:",(perf.auc@y.values))) + 
        xlab("Specificity") +
        ylab("Sensitivity") +
        theme(plot.title = element_text(size = 17)) 
}

rocplot((model1.prob), data2[test, ]$results)
posted @ 2018-01-16 18:07  嘻呵呵  阅读(5595)  评论(0编辑  收藏  举报