Visual tests currently differ between R-release and R-devel. This is a known issue linked with a bug fix in upstream R (r-lib/vdiffr#86). This commit updates the test and skips them in R-release.
1 |
|
|
2 |
#' Returns the coords as a data.frame in the right ordering for ggplot2
|
|
3 |
get.coords.for.ggplot <- function(roc) { |
|
4 | 1 |
df <- coords(roc, "all", transpose = FALSE) |
5 | 1 |
return(df[rev(seq(nrow(df))),]) |
6 |
}
|
|
7 |
|
|
8 |
get.aes.for.ggplot <- function(roc, legacy.axes, extra_aes = c()) { |
|
9 |
# Prepare the aesthetics
|
|
10 | 1 |
if(roc$percent) { |
11 | 1 |
if (legacy.axes) { |
12 |
aes_list <- list(x = "1-specificity", y = "sensitivity") |
|
13 |
xlims <- ggplot2::scale_x_continuous(lim=c(0, 100)) |
|
14 |
}
|
|
15 |
else { |
|
16 | 1 |
aes_list <- list(x = "specificity", y = "sensitivity") |
17 | 1 |
xlims <- ggplot2::scale_x_reverse(lim=c(100, 0)) |
18 |
}
|
|
19 |
}
|
|
20 |
else { |
|
21 | 1 |
if (legacy.axes) { |
22 |
aes_list <- list(x = "1-specificity", y = "sensitivity") |
|
23 |
xlims <- ggplot2::scale_x_continuous(lim=c(0, 1)) |
|
24 |
}
|
|
25 |
else { |
|
26 | 1 |
aes_list <- list(x = "specificity", y = "sensitivity") |
27 | 1 |
xlims <- ggplot2::scale_x_reverse(lim=c(1, 0)) |
28 |
}
|
|
29 |
}
|
|
30 |
# Add extra aes
|
|
31 | 1 |
for (ae in extra_aes) { |
32 | 1 |
aes_list[[ae]] <- "name" |
33 |
}
|
|
34 | 1 |
aes <- do.call(ggplot2::aes_string, aes_list) |
35 |
|
|
36 | 1 |
return(list(aes=aes, xlims=xlims)) |
37 |
}
|
|
38 |
|
|
39 |
load.ggplot2 <- function() { |
|
40 | 1 |
if (! isNamespaceLoaded("ggplot2")) { |
41 | 1 |
message('You may need to call library(ggplot2) if you want to add layers, etc.') |
42 |
}
|
|
43 | 1 |
load.suggested.package("ggplot2") |
44 |
}
|
|
45 |
|
|
46 |
ggroc <- function(data, ...) { |
|
47 | 1 |
UseMethod("ggroc") |
48 |
}
|
|
49 |
|
|
50 |
ggroc.roc <- function(data, legacy.axes = FALSE, ...) { |
|
51 | 1 |
load.ggplot2()
|
52 |
# Get the roc data with coords
|
|
53 | 1 |
df <- get.coords.for.ggplot(data) |
54 |
|
|
55 |
# Prepare the aesthetics
|
|
56 | 1 |
aes <- get.aes.for.ggplot(data, legacy.axes) |
57 |
|
|
58 |
# Do the plotting
|
|
59 | 1 |
ggplot2::ggplot(df) + ggplot2::geom_line(aes$aes, ...) + aes$xlims |
60 |
}
|
|
61 |
|
|
62 |
ggroc.list <- function(data, aes = c("colour", "alpha", "linetype", "size", "group"), legacy.axes = FALSE, ...) { |
|
63 | 1 |
load.ggplot2()
|
64 | 1 |
if (missing(aes)) { |
65 | 1 |
aes <- "colour" |
66 |
}
|
|
67 | 1 |
aes <- sub("color", "colour", aes) |
68 | 1 |
aes <- match.arg(aes, several.ok = TRUE) |
69 |
|
|
70 |
# Make sure data is a list and every element is a roc object
|
|
71 | 1 |
if (! all(sapply(data, methods::is, "roc"))) { |
72 |
stop("All elements in 'data' must be 'roc' objects.") |
|
73 |
}
|
|
74 |
|
|
75 |
# Make sure percent is consistent
|
|
76 | 1 |
percents <- sapply(data, `[[`, "percent") |
77 | 1 |
if (!(all(percents) || all(!percents))) { |
78 |
stop("ROC curves use percent inconsistently and cannot be plotted together") |
|
79 |
}
|
|
80 |
|
|
81 |
# Make sure the data is a named list
|
|
82 | 1 |
if (is.null(names(data))) { |
83 |
names(data) <- seq(data) |
|
84 |
}
|
|
85 |
# Make sure names are unique:
|
|
86 | 1 |
if (any(duplicated(names(data)))) { |
87 |
stop("Names of 'data' must be unique") |
|
88 |
}
|
|
89 |
|
|
90 |
# Get the coords
|
|
91 | 1 |
coord.dfs <- sapply(data, get.coords.for.ggplot, simplify = FALSE) |
92 |
|
|
93 |
# Add a "name" colummn
|
|
94 | 1 |
for (i in seq_along(coord.dfs)) { |
95 | 1 |
coord.dfs[[i]]$name <- names(coord.dfs)[i] |
96 |
}
|
|
97 |
|
|
98 |
# Make a big data.frame
|
|
99 | 1 |
coord.dfs <- do.call(rbind, coord.dfs) |
100 | 1 |
coord.dfs$name <- factor(coord.dfs$name, as.vector(names(data))) |
101 |
|
|
102 |
# Prepare the aesthetics
|
|
103 | 1 |
aes.ggplot <- get.aes.for.ggplot(data[[1]], legacy.axes, aes) |
104 |
|
|
105 |
# Do the plotting
|
|
106 | 1 |
ggplot2::ggplot(coord.dfs, aes.ggplot$aes) + ggplot2::geom_line(...) + aes.ggplot$xlims |
107 |
}
|
Read our documentation on viewing source code .