forked from tidyverse/ggplot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale-.r
132 lines (100 loc) · 3.19 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Domain: raw, transformed, user (limits)
# Range: raw, transformed
Scale <- proto(TopLevel, expr={
.input <- ""
.output <- ""
common <- NULL
legend <- TRUE
limits <- NULL
doc <- TRUE
class <- function(.) "scale"
new <- function(., name="Unknown") {
.$proto(name=name)
}
clone <- function(.) {
as.proto(.$as.list(all.names=TRUE), parent=.)
}
trained <- function(.) {
!is.null(.$input_set())
}
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 --------------------------------------------------------------------
breaks <- NULL
input <- function(.) .$.input
input_set <- function(.) {
nulldefault(.$limits, .$.domain)
}
# Output -------------------------------------------------------------------
output <- function(.) .$.output
output_breaks <- function(.) .$map(.$input_breaks())
output_expand <- function(.) {
expand_range(.$output_set(), .$.expand[1], .$.expand[2])
}
# Train scale from a data frame
train_df <- function(., df) {
if (!is.null(.$limits)) return()
input <- .$input()
if (length(input) == 1 && input %in% c("x", "y")) {
matches <- grep(paste("^", input, sep =""), names(df))
input <- names(df)[matches]
}
l_ply(input, function(var) .$train(df[[var]]))
}
# 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
}
# Guides
# ---------------------------------------------
legend_desc <- function(.) {
if (identical(., Scale) || !.$legend) return()
breaks <- .$output_breaks()
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
)
}
pprint <- function(., newline=TRUE) {
clist <- function(x) paste(x, collapse=",")
cat("scale_", .$objname, ": ", clist(.$input()), " -> ", clist(.$output()), sep="")
if (!is.null(.$input_set())) {
cat(" (", clist(.$input_set()), " -> ", clist(.$output_set()), ")", sep="")
}
if (newline) cat("\n")
}
html_returns <- function(.) {
ps(
"<h2>Returns</h2>\n",
"<p>This function returns a scale object.</p>"
)
}
my_names <- function(.) {
ps(.$class(), .$common, .$objname, sep="_", collapse=NULL)
}
my_full_name <- function(.) {
ps(.$class(), .$input(), .$objname, sep="_", collapse=NULL)
}
parameters <- function(.) {
params <- formals(get("new", .))
params[setdiff(names(params), c(".","variable"))]
}
})