2.2 The structure of ggplot calls
A generic template for creating a graph with the
ggplot() function has the following structure:
# Generic ggplot template: ggplot(data = <DATA>) + # 1. specify data set to use <GEOM_fun>(mapping = aes(<MAPPING>), # 2. specify geom + mappings <arg_1 = val_1, ..., arg_n = val_n>) + # - optional arguments to geom ... # - additional geoms + mappings <FACET_fun> + # - optional facet function <LOOK_GOOD_fun> # - optional themes, colors, labels, etc.
The generic template includes the following parts:
<DATA>is a data frame or tibble that contains the data that is to be plotted.
<GEOM_fun>is a function that maps data to a geometric object (“geom”) according to an aesthetic mapping that is specified in
aes(<MAPPING>). (A mapping specifies a relation between 2 entities. Here, the mapping specifies the correspondence of variables to graphical elements, i.e., what goes where.)
- A geom’s visual appearance (e.g., colors, shapes, sizes, …) can be customized
- in the aesthetic mapping (when varying visual features according to data properties), or
- by setting its arguments to specific values in
<arg_1 = val_1, ..., arg_n = val_n>(when remaining constant).
<FACET_fun>uses one or more variable(s) to split a complex plot into multiple subplots.
A sequence of optional
<LOOK_GOOD_fun>adjust the visual features of plots (e.g., by adding titles and text labels, color scales, plot themes, or setting coordinate systems).
Actually, a lot of the generic template is not necessary for using
ggplot() for generating a graph. A minimal template reduces to the following structure:
# Minimal ggplot template: ggplot(<DATA>) + # 1. specify data set to use <GEOM_fun>(aes(<MAPPING>) # 2. specify geom + mappings
A comparison of the generic and the minimal templates shows that many parts of a typical
ggplot() command are optional. In fact, the bare essentials only include some
<DATA>, at least one
<GEOM_fun>, and its required mappings in
aes(<MAPPING>). The rest is additional fluff, but — just as in human beings and other animals — adding fluff can have a major impact on a creature’s appearance and popularity.