R/plot.performance_roc.R

Defines functions plot.see_performance_roc

Documented in plot.see_performance_roc

#' Plot method for ROC curves
#'
#' The `plot()` method for the `performance::performance_roc()`
#' function.
#'
#' @inheritParams data_plot
#'
#' @return A ggplot2-object.
#'
#' @examples
#' library(performance)
#' data(iris)
#' set.seed(123)
#' iris$y <- rbinom(nrow(iris), size = 1, .3)
#'
#' folds <- sample(nrow(iris), size = nrow(iris) / 8, replace = FALSE)
#' test_data <- iris[folds, ]
#' train_data <- iris[-folds, ]
#'
#' model <- glm(y ~ Sepal.Length + Sepal.Width, data = train_data, family = "binomial")
#' result <- performance_roc(model, new_data = test_data)
#' result
#' plot(result)
#' @export
plot.see_performance_roc <- function(x, ...) {
  if (length(unique(x$Model)) > 1L) {
    p <- ggplot(
      x,
      aes(
        x = .data$Specificity,
        y = .data$Sensitivity,
        colour = .data$Model
      )
    )
  } else {
    p <- ggplot(
      x,
      aes(
        x = .data$Specificity,
        y = .data$Sensitivity
      )
    )
  }


  p +
    geom_abline(slope = 1, intercept = 0, linetype = "dashed", alpha = 0.5) +
    geom_line() +
    ylim(c(0, 1)) +
    xlim(c(0, 1)) +
    labs(
      x = "1 - Specificity (False Positive Rate)",
      y = "Sensitivity (True Positive Rate)",
      colour = NULL
    )
}

Try the see package in your browser

Any scripts or data that you put into this service are public.

see documentation built on Nov. 3, 2023, 5:10 p.m.