A layer is made up of five components:
Data is obviously a critical part of the plot, but it is important to remember that it is independent from the other components: we can construct a graphic that can be applied to multiple data sets. Along with the data, we need a specification of which variables are mapped to which aesthetics. For example, we might map weight to x position, height to y position and age to size. The details of the mapping are described by the scales. Choosing a good mapping is crucial for generating a useful graphic. More details of the how this works are available in the aes documentation.
These five components are described in R code with the layer function, which has the following form:
layer(geom, geom_params, stat, stat_params, data, aesthetics, position, params, ...)
When specifying geoms, stats and positions, you pass in the name of the object. For example, if you wanted to use geom_point, you would type geom="point"
. Similarly if you wanted stat_bin or position_fill, you would type stat="bin"
or position="fill"
If you only specify one of geom or stat, then the default will be used. If you don't specified position, then the default from the geom will be used.
The layer function is quite complicated as it needs to be able to deal with parameter specifications in a variety of formats. You can specify all the parameters (for the geom, stat and position) in one list, params
, as additional arguments to the layer function (ie. in ...), or individually to geom_params
and stat_params
.
Most of the time you won't use this function directly, but will instead use one of the short cuts described below.