I'm following along with the internals of the ggplot2
library and I'm trying to understand how non-positional aesthetics get mapped to the values that get passed to grid
. The book describes this process as
The last part of the data transformation is to train and map all non-positional aesthetics, i.e. convert whatever discrete or continuous input that is mapped to graphical parameters such as colours, linetypes, sizes etc."
However, this is the first time that the idea of "training" the data appears in the text.
The code for this process (from ggplot2:::ggplot_build.ggplot
) appears to be:
# Train and map non-position scales and guides
npscales <- scales$non_position_scales()
if (npscales$n() > 0) {
lapply(data, npscales$train_df)
plot$guides <- plot$guides$build(npscales, plot$layers, plot$labels, data)
data <- lapply(data, npscales$map_df)
} else {
# Only keep custom guides if there are no non-position scales
plot$guides <- plot$guides$get_custom()
}
but I'm unable to follow along with what's actually happening here. Does the lapply(data, npscales$train_df)
actually do anything? It doesn't seem to be saved and I would've expected it to be data <- lapply(data, npscales$train_df)
instead, but the function seems to always return NULLs no matter what plot I try it with.
What does "training" non-positional data mean in the ggplot2 package?
lapply
loop and the result isn't assigned, there are two possible reasons: (i) the function in the loop is called for a side-effect or (ii) it is done to force evaluation of something (which is a specific type effect). Personally, I prefer using afor
loop for such purposes. – Kenzie