在ROC图上使用scale_x_reverse

时间:2022-12-06 23:58:23

I tried to reproduce ROC curve from the plot.roc function from pRoc package with ggplot2.

我试图从图中再现ROC曲线。roc函数来自带有ggplot2的pRoc包。

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 (%)')

The plot.roc produces: 在ROC图上使用scale_x_reverse

的阴谋。中华民国产生:

While the ggplot2 produces: 在ROC图上使用scale_x_reverse

而ggplot2产生:

scale_x_reverse() seems to be the problem, is there any other way to reverse the X axis or to correct that plot?

scale_x_reverse()似乎是问题所在,是否有其他方法来反转X轴或者修正这个图?

1 个解决方案

#1


3  

You can use geom_path instead of geom_step:

你可以使用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 (%)')

在ROC图上使用scale_x_reverse

#1


3  

You can use geom_path instead of geom_step:

你可以使用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 (%)')

在ROC图上使用scale_x_reverse