/
aes.r
97 lines (84 loc) · 3.15 KB
/
aes.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
# dput(c(names(.base_to_ggplot), "group","order", "z", sort(unique(unlist(sapply(Geom$find_all(), function(y) c(names(y$default_aes()), y$required_aes)))))))
.all_aesthetics <- c("col", "color", "pch", "cex", "lty", "lwd", "srt", "adj", "bg", "fg", "group", "order", "z", "angle", "colour", "fill", "height",
"hjust", "intercept", "label", "linetype", "shape", "size", "slope", "vjust", "weight", "width", "x", "xend", "xmax", "xmin", "y", "yend", "ymax", "ymin")
.base_to_ggplot <- c(
"col" = "colour",
"color" = "colour",
"pch" = "shape",
"cex" = "size",
"lty" = "linetype",
"lwd" = "size",
"srt" = "angle",
"adj" = "hjust",
"bg" = "fill",
"fg" = "colour",
"min" = "ymin",
"max" = "ymax"
)
# Generate aesthetic mappings
# Aesthetic mappings describe how variables in the data are mapped to visual properties (aesthetics) of geoms.
#
# aes creates a list of unevaluated expressions. This function also performs
# partial name matching, converts color to colour, and old style R names to
# new ggplot names (eg. pch to shape, cex to size)
#
# @arguments x value
# @arguments y value
# @arguments List of name value pairs
# @keyword internal
# @alias str.uneval
# @alias print.uneval
# @seealso \code{\link{aes_string}}
#X aes(x = mpg, y = wt)
#X aes(x = mpg ^ 2, y = wt / cyl)
aes <- function(x, y, ...) {
aes <- structure(as.list(match.call()[-1]), class="uneval")
aes <- rename_aes(aes)
new_names <- lapply(names(aes), function(x) {
m <- charmatch(x, .all_aesthetics)
if (is.na(m) || m == 0) x else .all_aesthetics[m]
})
names(aes) <- new_names
aes
}
rename_aes <- function(x) {
rename(x, .base_to_ggplot)
}
aes_to_scale <- function(var) {
if (var %in% c("x", "xmin", "xmax", "xend")) return("x")
if (var %in% c("y", "ymin", "ymay", "yend")) return("y")
var
}
# Generate aesthetic mappings from a string
# Aesthetic mappings describe how variables in the data are mapped to visual properties (aesthetics) of geoms. Compared to aes this function operates on strings rather than expressions.
#
# \code{aes_string} is particularly useful when writing functions that create
# plots because you can use strings to define the aesthetic mappings, rather
# than having to mess around with expressions.
#
# @arguments List of name value pairs
# @keyword internal
# @seealso \code{\link{aes}}
#X aes_string(x = "mpg", y = "wt")
#X aes(x = mpg, y = wt)
aes_string <- function(...) {
structure(rename_aes(lapply(list(...), function(x) parse(text=x)[[1]])),
class="uneval")
}
print.uneval <- function(x, ...) str(unclass(x))
str.uneval <- function(object, ...) str(unclass(object), ...)
# Aesthetic defaults
# Convenience method for setting aesthetic defaults
#
# @arguments values from aesthetic mappings
# @arguments defaults
# @arguments user specified values
# @value a data.frame, with all factors converted to character strings
# @keyword internal
aesdefaults <- function(data, y., params.) {
updated <- updatelist(y., params.)
df <- as.data.frame(tryapply(defaults(data, updated), function(x) eval(x, data, globalenv())))
factors <- sapply(df, is.factor)
df[factors] <- lapply(df[factors], as.character)
df
}