【问题标题】:ggplot2: use of scale_x_reverse on ROC plotggplot2:在 ROC 图上使用 scale_x_reverse
【发布时间】:2016-09-23 03:15:59
【问题描述】:

我尝试从pRoc 包中的plot.roc 函数和ggplot2 重现ROC 曲线。

library(mlbench)
library(caret)

data(Sonar)

set.seed(998)
fitControl <- trainControl(method = "repeatedcv",
                           number = 10,
                           repeats = 10,
                           ## Estimate class probabilities
                           classProbs = TRUE,
                           ## Evaluate performance using 
                           ## the following function
                           summaryFunction = twoClassSummary)

gbmGrid <-  expand.grid(interaction.depth = c(1, 5, 9),
                        n.trees = (1:30)*50,
                        shrinkage = 0.1,
                        n.minobsinnode = 20)

inTraining <- createDataPartition(Sonar$Class, p = .75, list = FALSE)
training <- Sonar[ inTraining,]
testing  <- Sonar[-inTraining,]


set.seed(825)
gbmFit <- train(Class ~ ., data = training,
                method = "gbm",
                trControl = fitControl,
                verbose = FALSE,
                tuneGrid = gbmGrid,
                ## Specify which metric to optimize
                metric = "ROC")
gbmFit

probs = predict(gbmFit, newdata = testing, type = "prob")

roc = roc(predictor = probs$M,
          response = testing$Class,
          levels = c('M','R'),
          percent = TRUE)
plot.roc(roc, print.auc = TRUE, col='red')


df = data.frame(Specificity=roc$specificities, Sensitivity=roc$sensitivities)
ggplot(data = df, aes(x = Specificity, y = Sensitivity))+
    geom_step(color='red', size=2, direction = "hv")+
    scale_x_reverse()+
    geom_abline(intercept = 100, slope = 1, color='grey')+
    annotate("text", x = 30, y = 20, label = paste0('AUC: ', round(roc$auc,1), '%'), size = 8)+
    ylab('Sensitivity (%)')+
    xlab('Specificity (%)')

plot.roc 产生:

虽然ggplot2 产生:

scale_x_reverse() 似乎是问题所在,有没有其他方法可以反转 X 轴或纠正该图?

【问题讨论】:

    标签: r ggplot2 roc


    【解决方案1】:

    您可以使用geom_path 代替geom_step

    ggplot(data = df, aes(x = Specificity, y = Sensitivity))+
      geom_path(colour = 'red', size = 2)+
      scale_x_reverse() +
      geom_abline(intercept = 100, slope = 1, color='grey')+
      annotate("text", x = 30, y = 20, label = paste0('AUC: ', round(roc$auc,1), '%'), size = 8)+
      ylab('Sensitivity (%)')+
      xlab('Specificity (%)')
    

    【讨论】:

    • 不错,但geom_step 问题可能是那个geom 的一个错误。
    • @MikeWise,如果它是一个错误,它只会出现在geom_stepscale_x_reverse 的组合中。您可以在没有scale_x_reverse 的情况下尝试我的代码,并且没有图形问题。
    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2013-04-08
    • 2020-06-12
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多