/
position-.r
54 lines (42 loc) · 1.34 KB
/
position-.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
# Position adjustment occurs over all groups within a geom
# They work only with discrete x scales and may affect x and y position.
# Should occur after statistics and scales have been applied.
Position <- proto(TopLevel, expr = {
adjust <- function(., data, scales, ...) data
class <- function(.) "position"
width <- NULL
height <- NULL
new <- function(., width = NULL, height = NULL) {
.$proto(width = width, height = NULL)
}
parameters <- function(.) {
params <- formals(get("new", .))
params[setdiff(names(params), c("."))]
}
pprint <- function(., newline=TRUE) {
cat("position_", .$objname, ": ()", sep="")
if (newline) cat("\n")
}
html_returns <- function(.) {
ps(
"<h2>Returns</h2>\n",
"<p>This function returns a position object.</p>"
)
}
parameters <- function(.) {
params <- formals(get("new", .))
params[setdiff(names(params), c(".","variable"))]
}
})
# Convenience function to ensure that all position variables
# (x, xmin, xmax, xend) are transformed in the same way
transform_position <- function(df, trans_x = NULL, trans_y = NULL) {
scales <- aes_to_scale(names(df))
if (!is.null(trans_x)) {
df[scales == "x"] <- lapply(df[scales == "x"], trans_x)
}
if (!is.null(trans_y)) {
df[scales == "y"] <- lapply(df[scales == "y"], trans_y)
}
df
}