ecaes: Construct aesthetic mappings

Description Usage Arguments Details Value Quasiquotation See Also Examples

Description

Aesthetic mappings describe how variables in the data are mapped to visual properties (aesthetics) of option.

Usage

1
ecaes(x, y, ...)

Arguments

x, y, ...

List of name-value pairs in the form 'aesthetic = variable' describing which variables in the data should be mapped to which aesthetics used by the option. The expression 'variable' is evaluated within the data, so there is no need to refer to the original dataset (i.e., use 'ecaes(variable)' instead of 'ecaes(df$variable)'). The names for x and y aesthetics are typically omitted because they are so common; all other aesthetics must be named.

Details

This function also standardises aesthetic names by converting 'colour' to 'color'.

Value

A list with class 'uneval' and 'ecaes'. Components of the list are either quosures or constants.

Quasiquotation

'ecaes()' is a [quoting function][rlang::quotation]. This means that its inputs are quoted to be evaluated in the context of the data. This makes it easy to work with variables from the data frame because you can name those directly. The flip side is that you have to use [quasiquotation][rlang::quasiquotation] to program with 'ecaes()'. See a tidy evaluation tutorial such as the [dplyr programming vignette](http://dplyr.tidyverse.org/articles/programming.html) to learn more about these techniques.

See Also

[aes()] Construct aesthetic mappings for ggplot.

Examples

 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
## Not run: 
ecaes(x = mpg, y = wt)
ecaes(mpg, wt)

# You can also map aesthetics to functions of variables
ecaes(x = mpg ^ 2, y = wt / cyl)

# Or to constants
ecaes(x = 1, color = "smooth")

# Aesthetic names are automatically standardised
ecaes(col = x)
ecaes(fg = x)
ecaes(color = x)
ecaes(colour = x)
ecaes(shape = y)
ecaes(size = z)

# Tidy evaluation ----------------------------------------------------
# ecaes() automatically quotes all its arguments.
scatter_by <- function(data, ...) {
  echart() %>% ec_add_series(data, type = 'scatter', mapping = ecaes(...))
}
scatter_by(mtcars, disp, drat)

# If your wrapper has a more specific interface with named arguments,
# you need "enquote and unquote":
scatter_by <- function(data, x, y) {
  x <- enquo(x)
  y <- enquo(y)

  echart() %>%
    ec_add_series(data, type = 'scatter', mapping = ecaes(!!x, !!y))
}
scatter_by(mtcars, disp, drat)

# Note that users of your wrapper can use their own functions in the
# quoted expressions and all will resolve as it should!
cut3 <- function(x) cut_number(x, 3)
scatter_by(mtcars, cut3(disp), drat)

## End(Not run)

jeevanyue/echarter documentation built on Oct. 16, 2020, 5:12 a.m.