-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add geom_polygon_auc function (issue #129)
- Loading branch information
Showing
9 changed files
with
367 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
geom_polygon_auc <- function(data, ...) { | ||
UseMethod("geom_polygon_auc") | ||
} | ||
|
||
geom_polygon_auc.auc <- function(data, legacy.axes = FALSE, ...) { | ||
# Get the roc data with coords | ||
roc <- attr(data, "roc") | ||
roc$auc <- data | ||
df <- get.coords.for.ggplot(roc, ignore.partial.auc = FALSE) | ||
|
||
# Add bottom-right point | ||
partial.auc <- attr(data, "partial.auc") | ||
one.or.hundred <- ifelse(attr(data, "percent"), 100, 1) | ||
if (legacy.axes) { | ||
if (identical(partial.auc, FALSE)) { | ||
df[nrow(df) + 1, ] <- c(NA, one.or.hundred, 0, one.or.hundred) | ||
} | ||
else if (attr(data, "partial.auc.focus") == "sensitivity") { | ||
df[nrow(df) + c(1, 2), ] <- c(NA, NA, one.or.hundred, one.or.hundred, partial.auc, one.or.hundred, one.or.hundred) | ||
} | ||
else { # partial.auc.focus == "specificity" | ||
df[nrow(df) + c(1, 2), ] <- c(NA, NA, rev(partial.auc), 0, 0, one.or.hundred - rev(partial.auc)) | ||
} | ||
} | ||
else { | ||
if (identical(partial.auc, FALSE)) { | ||
df[nrow(df) + 1, ] <- c(NA, 0, 0, 0) | ||
} | ||
else if (attr(data, "partial.auc.focus") == "sensitivity") { | ||
df[nrow(df) + c(1, 2), ] <- c(NA, NA, 0, 0, partial.auc, 0, 0) | ||
} | ||
else { # partial.auc.focus == "specificity" | ||
df[nrow(df) + c(1, 2), ] <- c(NA, NA, rev(partial.auc), 0, 0, one.or.hundred - rev(partial.auc)) | ||
} | ||
} | ||
|
||
# Prepare the aesthetics | ||
aes <- get.aes.for.ggplot(attr(data, "roc"), legacy.axes) | ||
|
||
# Do the plotting | ||
ggplot2::geom_polygon(aes$aes, data=df, ...) | ||
} | ||
|
||
geom_polygon_auc.roc <- function(data, ...) { | ||
geom_polygon_auc(data$auc, ...) | ||
} | ||
|
||
geom_polygon_auc.smooth.roc <- geom_polygon_auc.roc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
\encoding{UTF-8} | ||
\name{geom_polygon_auc} | ||
\alias{geom_polygon_auc.auc} | ||
\alias{geom_polygon_auc.roc} | ||
\alias{geom_polygon_auc.smooth.roc} | ||
\alias{geom_polygon_auc} | ||
|
||
\title{ | ||
Add an AUC polygon to a ggroc plot | ||
} | ||
\description{ | ||
EXPERIMENTAL - Add an AUC polygon to a ggroc plot. | ||
} | ||
\usage{ | ||
\S3method{geom_polygon_auc}{roc}(data, legacy.axes = FALSE, ...) | ||
\S3method{geom_polygon_auc}{smooth.roc}(data, legacy.axes = FALSE, ...) | ||
} | ||
|
||
\arguments{ | ||
\item{data}{a roc object from the \link{roc} function, same as the one | ||
used to build the ggroc initially. | ||
} | ||
\item{legacy.axes}{must match the value given to \code{ggroc}. | ||
} | ||
\item{...}{additional aesthetics for \code{\link[ggplot2:geom_polygon]{geom_polygon}} | ||
to set: \code{alpha}, \code{colour}, \code{linetype} and \code{linewidth}. | ||
} | ||
} | ||
|
||
\details{ | ||
} | ||
|
||
|
||
\seealso{ | ||
\code{\link{ggroc}} | ||
} | ||
\examples{ | ||
|
||
# Create a ROC curve: | ||
data(aSAH) | ||
roc.s100b <- roc(aSAH$outcome, aSAH$s100b) | ||
roc.s100b.percent <- roc(aSAH$outcome, aSAH$s100b, percent = TRUE) | ||
|
||
ggroc(roc.s100b) + geom_polygon_auc(roc.s100b$auc) | ||
|
||
# legacy.axes must be repeated | ||
ggroc(roc.s100b.percent, legacy.axes=TRUE) + geom_polygon_auc(roc.s100b.percent, legacy.axes=TRUE) | ||
|
||
# Partial AUCs | ||
auc.s100b.partial.sp <- auc(roc.s100b, partial.auc = c(0.9, 1)) | ||
auc.s100b.partial.se <- auc(roc.s100b, partial.auc = c(0.8, 0.9), partial.auc.focus="se") | ||
|
||
ggroc(roc.s100b) + geom_polygon_auc(auc.s100b.partial.sp) | ||
ggroc(roc.s100b) + geom_polygon_auc(auc.s100b.partial.se) | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
tests/testthat/_snaps/geom_polygon_auc/geom-polygon-auc-partial-screenshot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.