I have a list of aesthetics constructed programmatically and passed from other functions. They may contain any combination of aesthetics like alpha, colour, fill, etc., which is not know beforehand.
my_aes_list <- list(
x = "cty",
y = "hwy",
col = "trans")
I can use aes_string
to create aesthetics programmatically from this list:
library(ggplot2)
my_aes <- do.call(ggplot2::aes_string, my_aes_list)
ggplot(mpg) + geom_point(my_aes)
However aes_string()
was deprecated in ggplot2 3.0.0 and I would like to replace this code before it gets removed in a future version.
The documented alternative and several stack overflow questions indicate to use the .data[[
pronoun. However I can't find out how to use it with arbitrary programmatic aesthetics.
Doing something like:
my_data_list <- list()
for (name in names(my_aes_list)) {
my_data_list[[name]] <- .data[[my_aes_list[name]]]
}
obviously doesn't work outside of the aes
call itself. I also tried the !!!
injectors to no avail:
ggplot(mpg) + geom_point(aes(!!!my_aes_list))
seems to work but doesn't plot correctly.
What's the proper way to replace aes_string
and call ggplot
in this context?