I have a function that optionally accepts predefined aesthetics (for ggplot2
). This can be in one of several forms, including
mapping <- aes(color = c(cyl, gear))
mapping
# Aesthetic mapping:
# * `colour` -> `c(cyl, gear)`
or programmatically as
mapping <- aes(color = c(.data[["cyl"]], .data[["gear"]]))
mapping
# Aesthetic mapping:
# * `colour` -> `c(.data[["cyl"]], .data[["gear"]])`
(I'm using color=
here, but really I'm using group=
and subgroup=
in various ways. I'm simplifying to color=
here for simplicity.)
My function cannot control if or how the caller defines mapping
(it defaults to NULL
), it can either be using symbols (first example) or one of the programmatic options like .data
(second example), but internally after I adjust/augment the data, I need to add a variable to that aesthetic.
I can combine different aesthetics using c
, as in
mapping <- aes(color = factor(cyl))
othermap1 <- aes(x = mpg, y = disp)
ggplot(mtcars, mapping = aes(!!!c(mapping, othermap1))) +
geom_path()
(The plot itself doesn't matter much, just that it works.)
If I want to add a variable to the color=
aesthetic, I don't know how to best do it.
othermap2 <- aes(color = gear)
I would like to be able to get the effect of color=
combined, with an result similar to the manual:
ggplot(mtcars, mapping = aes(mpg, disp, color = interaction(cyl, gear))) +
geom_path()
This is all programmatic, and I would prefer to allow the user to provide a "real" mapping=
argument (e.g., mapping=aes(...)
) in the call to my function.
I think one possible way is to require the user to provide a named-list of variables if they intend to assign color=
, such as mapping=list(group="cyl")
and internally I'll augment/merge and then create the .data[[x]]
aesthetics internally ... but I really would prefer to continue to allow the user to use mapping=aes(..)
.
You can use some rlang magic to manipulate the mapping. Briefly, we just assign the new aesthetic if it was abent, otherwise we create a new call combining the two expressions.
Some demonstrations
Created on 2023-12-22 with reprex v2.0.2