2.2 The structure of ggplot calls

A generic template for creating a graph with the ggplot() function has the following structure:

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
    1. in the aesthetic mapping (when varying visual features according to data properties), or
    2. by setting its arguments to specific values in <arg_1 = val_1, ..., arg_n = val_n> (when remaining constant).
  • An optional <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:

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.