Skip to content

Commit

Permalink
Pass multiple aes to ggroc (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
xrobin committed Mar 3, 2019
1 parent e47ae68 commit e7297e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
27 changes: 18 additions & 9 deletions R/ggroc.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,34 @@ get.coords.for.ggplot <- function(roc) {
return(df[rev(seq(nrow(df))),])
}

get.aes.for.ggplot <- function(roc, legacy.axes) {
get.aes.for.ggplot <- function(roc, legacy.axes, extra_aes) {
# Prepare the aesthetics
if(roc$percent) {
if (legacy.axes) {
aes <- ggplot2::aes_string(x = "1-specificity", y = "sensitivity")
aes_list <- list(x = "1-specificity", y = "sensitivity")
xlims <- ggplot2::scale_x_continuous(lim=c(0, 100))
}
else {
aes <- ggplot2::aes_string(x = "specificity", y = "sensitivity")
aes_list <- list(x = "specificity", y = "sensitivity")
xlims <- ggplot2::scale_x_reverse(lim=c(100, 0))
}
}
else {
if (legacy.axes) {
aes <- ggplot2::aes_string(x = "1-specificity", y = "sensitivity")
aes_list <- list(x = "1-specificity", y = "sensitivity")
xlims <- ggplot2::scale_x_continuous(lim=c(0, 1))
}
else {
aes <- ggplot2::aes_string(x = "specificity", y = "sensitivity")
aes_list <- list(x = "specificity", y = "sensitivity")
xlims <- ggplot2::scale_x_reverse(lim=c(1, 0))
}
}
# Add extra aes
for (ae in extra_aes) {
aes_list[[ae]] <- "name"
}
aes <- do.call(ggplot2::aes_string, aes_list)

return(list(aes=aes, xlims=xlims))
}

Expand All @@ -49,8 +55,12 @@ ggroc.roc <- function(data, legacy.axes = FALSE, ...) {
}

ggroc.list <- function(data, aes = c("colour", "alpha", "linetype", "size", "group"), legacy.axes = FALSE, ...) {
aes <- match.arg(aes)

if (missing(aes)) {
aes <- "colour"
}
aes <- sub("color", "colour", aes)
aes <- match.arg(aes, several.ok = TRUE)

# Make sure data is a list and every element is a roc object
if (! all(sapply(data, is, "roc"))) {
stop("All elements in 'data' must be 'roc' objects.")
Expand Down Expand Up @@ -83,8 +93,7 @@ ggroc.list <- function(data, aes = c("colour", "alpha", "linetype", "size", "gro
coord.dfs <- do.call(rbind, coord.dfs)

# Prepare the aesthetics
aes.ggplot <- get.aes.for.ggplot(data[[1]], legacy.axes)
aes.ggplot$aes[[aes]] <- as.symbol("name")
aes.ggplot <- get.aes.for.ggplot(data[[1]], legacy.axes, aes)

# Do the plotting
ggplot(coord.dfs, aes.ggplot$aes) + ggplot2::geom_line(...) + aes.ggplot$xlims
Expand Down
5 changes: 4 additions & 1 deletion man/ggroc.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
\arguments{
\item{data}{a roc object from the \link{roc} function, or a list of roc objects.
}
\item{aes}{the name of the aesthetics for \code{\link{geom_line}} to map to the different ROC curves supplied. Use \dQuote{group} if
\item{aes}{the name(s) of the aesthetics for \code{\link{geom_line}} to map to the different ROC curves supplied. Use \dQuote{group} if
you want the curves to appear with the same aestetic, for instance if you are faceting instead.}
\item{legacy.axes}{a logical indicating if the specificity axis (x
axis) must be plotted as as decreasing \dQuote{specificity}
Expand Down Expand Up @@ -74,6 +74,9 @@ 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")
Expand Down

0 comments on commit e7297e2

Please sign in to comment.