ggroc: Plot a ROC curve with ggplot2

View source: R/ggroc.R

ggroc.rocR Documentation

Plot a ROC curve with ggplot2

Description

This function plots a ROC curve with ggplot2.

Usage

## S3 method for class 'roc'
ggroc(data, legacy.axes = FALSE, ...)
## S3 method for class 'smooth.roc'
ggroc(data, legacy.axes = FALSE, ...)
## S3 method for class 'list'
ggroc(data, aes = c("colour", "alpha", "linetype", "linewidth", "size", "group"),
      legacy.axes = FALSE, ...)

Arguments

data

a roc object from the roc function, or a list of roc objects.

aes

the name(s) of the aesthetics for geom_line to map to the different ROC curves supplied.

legacy.axes

a logical indicating if the specificity axis (x axis) must be plotted as as decreasing “specificity” (FALSE, the default) or increasing “1 - specificity” (TRUE) as in most legacy software.

...

additional aesthetics for geom_line to set: alpha, colour, linetype, linewidth (new in ggplot2 3.4.0), and size (before ggplot2 3.4.0). The group aesthetic is always active since version 1.18.5 and is kept for backwards compatibility.

Details

This function initializes a ggplot object from a ROC curve (or multiple if a list is passed). It returns the ggplot with a line layer on it. You can print it directly or add your own layers and theme elements.

See Also

roc, plot.roc, ggplot2

Examples

# Create a basic roc object
data(aSAH)
rocobj <- roc(aSAH$outcome, aSAH$s100b)
rocobj2 <- roc(aSAH$outcome, aSAH$wfns)

if (require(ggplot2)) {
g <- ggroc(rocobj)
g
# with additional aesthetics:
ggroc(rocobj, alpha = 0.5, colour = "red", linetype = 2, size = 2)

# You can then your own theme, etc.
g + theme_minimal() + ggtitle("My ROC curve") + 
    geom_segment(aes(x = 1, xend = 0, y = 0, yend = 1), color="grey", linetype="dashed")

# And change axis labels to FPR/FPR
gl <- ggroc(rocobj, legacy.axes = TRUE)
gl
gl + xlab("FPR") + ylab("TPR") + 
    geom_segment(aes(x = 0, xend = 1, y = 0, yend = 1), color="darkgrey", linetype="dashed")

# Multiple curves:
g2 <- ggroc(list(s100b=rocobj, wfns=rocobj2, ndka=roc(aSAH$outcome, aSAH$ndka)))
g2

# This is equivalent to using roc.formula:
roc.list <- roc(outcome ~ s100b + ndka + wfns, data = aSAH)
g.list <- ggroc(roc.list)
g.list

# You can change the aesthetics as you normally would with ggplot2:
g.list + scale_colour_brewer(palette="RdGy")
g.list + scale_colour_manual(values = c("red", "blue", "black"))

# with additional aesthetics:
g3 <- ggroc(roc.list, linetype=2)
g3
g4 <- ggroc(roc.list, aes="linetype", color="red")
g4
# changing multiple aesthetics:
g5 <- ggroc(roc.list, aes=c("linetype", "color"))
g5

# OR faceting
g.list + facet_grid(.~name) + theme(legend.position="none")
}

pROC documentation built on Nov. 2, 2023, 6:05 p.m.