2.5 Noteworthy ggplot details

It is instructive to explicate some details that characterize and distinguish commands in ggplot2 (Wickham, Chang, et al., 2020) from other commands in the tidyverse (Wickham, 2019c):

  • ggplot requires data and maps independent variables to dimensions (e.g., the x- and y-axis) and dependent variables to geometric objects (called geoms). It typically assumes that the to-be-plotted <DATA> is in a table (data frame or tibble) in long format and contains independent variables as factors.

  • The arguments data = and mappings = can be omitted, but an aesthetic mapping aes(<MAPPING>) for at least one geom is needed.

  • Different geoms can be combined into layers, but their order matters (as later layers are printed on top of earlier ones).

  • When multiple geoms use the same aesthetic mappings, their common aes(<MAPPING>) can be moved into the initial ggplot call (behind <DATA>).

  • In a ggplot call, a sequence of commands is combined by +, rather than %>%, which is the forward pipe operator provided by the magrittr package (Bache & Wickham, 2014). The + has to be at the end of the current line, rather than at the beginning of the next line. (See Section 3.3 for more information about the forward pipe operator.)

Both the content and the visual appearance of plots are highly customizable (e.g., by supplying many aesthetic arguments, combining multiple geoms in one plot, speciying labels and legends, and applying pre-defined themes to plots). Tuning plots can be a lot of fun (see Section 4.2.11 for some examples), but always keep in mind your current goals and the plot’s intended audience.


Bache, S. M., & Wickham, H. (2014). magrittr: A forward-pipe operator for R. Retrieved from https://CRAN.R-project.org/package=magrittr

Wickham, H. (2019c). tidyverse: Easily install and load the ’tidyverse’. Retrieved from https://CRAN.R-project.org/package=tidyverse

Wickham, H., Chang, W., Henry, L., Pedersen, T. L., Takahashi, K., Wilke, C., … Dunnington, D. (2020). ggplot2: Create elegant data visualisations using the grammar of graphics. Retrieved from https://CRAN.R-project.org/package=ggplot2