forked from tidyverse/ggplot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale-.r
108 lines (86 loc) · 2.63 KB
/
scale-.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Scale <- proto(TopLevel, expr={
.input <- ""
.output <- ""
.reverse <- FALSE
common <- NULL
legend <- TRUE
class <- function(.) "scale"
new <- function(., name="Unknown") {
.$proto(name=name)
}
discrete <- function(.) FALSE
clone <- function(.) {
as.proto(.$as.list(), parent=.)
}
find <- function(., output, only.documented = FALSE) {
scales <- Scales$find_all()
select <- sapply(scales, function(x) any(output %in% c(x$output(), get("common", x))))
if (only.documented) select <- select & sapply(scales, function(x) get("doc", x))
unique(scales[select])
}
input <- function(.) .$.input
output <- function(.) .$.output
domain <- function(.) .$.domain
# Train scale from a data frame
train_df <- function(., df) {
.$train(df[[.$input()]])
}
transform_df <- function(., df) {
input <- .$input()
output <- .$output()
transform <- function(var) .$stransform(df[, var])
if (length(input) == 1 && input == "y") {
input <- output <- intersect(c("y","min", "max"), names(df))
}
df <- do.call("data.frame", lapply(input, transform))
if (ncol(df) == 0) return(NULL)
names(df) <- output
df
}
# Map values from a data.frame. Returns data.frame
map_df <- function(., df) {
input <- df[[.$input()]]
if (is.null(input)) {
# stop("scale_", .$objname, ": no ", .$input(), " mapping in plot", call.=FALSE)
output <- data.frame()
} else {
output <- data.frame(.$map(input))
}
if (ncol(output) > 0) names(output) <- .$output()
output
}
pprint <- function(., newline=TRUE) {
clist <- function(x) paste(x, collapse=",")
cat("scale_", .$objname, ": ", clist(.$input()), " -> ", clist(.$output()), sep="")
if (!is.null(.$domain())) {
cat(" (", clist(.$domain()), " -> ", clist(.$frange()), ")", sep="")
}
if (newline) cat("\n")
}
html_returns <- function(.) {
ps(
"<h2>Returns</h2>\n",
"<p>This function returns a scale object.</p>"
)
}
# Guides
# ---------------------------------------------
legend_desc <- function(.) {
if (identical(., Scale) || !.$legend) return()
breaks <- .$rbreaks()
labels <- .$labels()
if (is.null(breaks) || is.null(labels)) return()
df <- data.frame(breaks, stringsAsFactors = FALSE)
names(df) <- .$output()
df$label <- labels
list(
name = nulldefault(.$name, ""),
aesthetic = .$output(),
display = df
)
}
parameters <- function(.) {
params <- formals(get("new", .))
params[setdiff(names(params), c(".","variable"))]
}
})