9 Visualize with ggplot2

This chapter introduces data visualization with the R package ggplot2 (Wickham, Chang, et al., 2024). Essentially, ggplot2 provides an abstract language and powerful toolbox for creating scientific visualizations. If R was not already awesome in itself, ggplot2 would make it worthwhile to learn it.

Please note: Although this chapter is quite long, it is still incomplete.

Preflections

i2ds: Preflexions

Each element of a visualization (e.g., a line or shape) has both a form (aesthetic or visual appearance) and serves a function (goal or purpose). The following questions address the difference between form and function the context of visualizations:

  • What are common functional elements of visualizations?
  • What is the relation between data and those elements?
  • What are aesthetic features of visualizations?
  • How can aesthetic features reflect or emphasize features of the data?

Whereas the form of visual elements seems obvious (but includes features like color, shape, or size), their function and relation to data remains abstract and difficult to describe. But abstraction is helpful for discovering the patterns or principles that organize some phenomenon. A useful heuristic for identifying functions of visual elements is asking questions like: “What does this element (aim to) show?” and “How does it achieve this?”

9.1 Introduction

The ggplot2 package (Wickham, Chang, et al., 2024) and the corresponding book ggplot2: Elegant graphics for data analysis (Wickham, 2016) provide an implementation of The Grammar of Graphics (Wilkinson, 2005), which develops a systematic way of thinking about — or a language and philosophy of — data visualization. The notion of a “grammar” is one that we are familiar with (e.g., when studying a foreign language), but its exact meaning remains difficult to define. Wilkinson (2005) notes that a grammar provides the rules that make languages expressive. The essence of a grammar is to specify how elementary components can be combined to create well-formed expressions. Thus, knowing the grammar of a language allows us to combine elementary concepts (e.g., nouns and verbs) into sentences (e.g., assertions and questions) to express some meaning (e.g., “I am happy.”, “Is this fun?”). Similarly, learning the grammar of graphics will allow us to express aspects of our data by creating visualizations.

Learning how to use ggplot2 is — just like learning a new language — a journey, rather than a destination. Just as we can learn and use sentences of a foreign language without being fully aware of its grammatical rules, we will start using the functions of ggplot2 to visualize data without understanding all details. Hence, we should not be surprised if some concepts and relations remain somewhat obscure for a while. Fortunately, there is no need to understand all about the ggplot() function to create awesome visualizations with it.

9.1.0.1 Terminology

Distinguishing ggplot2 from ggplot():

  • ggplot and ggplot2 denote R packages (currently in its version ggplot2 3.5.1), whereas

  • ggplot() is the main function of those packages for generating a visualization.

Beyond this technical distinction, the grammar of graphics includes many new concepts:

  • mapping data variables to visual aspects or dimensions (e.g., axes, groups);

  • distinguish a range of geoms (i.e., geometric objects, e.g., areas, bars, lines, points) that transform data via statistics (stat arguments or stat_* functions);

  • aesthetic features (e.g., colors, shapes, sizes) and descriptive elements (e.g., text captions, labels, legend, titles);

  • combining graphical elements into layers and viewing different facets of a visualization.

We will explain those terms when we encounter and need them, but using their corresponding functions is more important than explicit knowledge of their definitions.

9.1.1 Contents

This chapter provides an introduction to the ggplot2 package (Wickham, Chang, et al., 2024). It covers some basic types of visualizations (e.g., histograms, bar charts, box plots, line plots, and scatterplots), shows how they can be improved by adding aesthetic features (e.g., colors, labels, and themes),
and discusses more advanced aspects (e.g., by combining layers, using facets, and extensions).

The following table provides a first mapping of visualization tasks to common types of visualizations. Importantly, we organize this chapter by visualization tasks, rather than visualization types. The reason for this is quite simple: Multiple types of visualizations can solve the same task.

Common visualization tasks and types.
Task: Visualize… Type of visualization In ggplot2
distributions histogram ?
summaries bar chart ?
box plot ?
relations scatterplot ?
line plot ?
trend line ?

The question marks in the final column of the table require ggplot2 functions that solve the task at hand by creating a corresponding type of visualization. The bulk of this chapter will introduce geometric objects (so-called geoms) that create some type of visualization. Each geom function comes with some required and some optional arguments that can either be set to constant values or mapped to a data variable. Thus, learning the language of ggplot2 involves some knowledge of its grammar and vocabulary. While the grammar requires some understanding of the layered structure of visualizations, the vocabularly mostly consists of geoms and their required arguments.

9.1.2 Data and tools

This chapter primarily uses the functions of the ggplot2 package:

but also some related packages:

library(patchwork)  # for combining and arranging plots
library(unikn)      # for colors and color functions 

In addition to using data from the datasets and ggplot2 packages, we use the penguins dataset from the palmerpenguins package (Horst et al., 2022):

library(palmerpenguins)  # for penguins data
Meet the penguins of the Palmer Archipelago, Antarctica. (Artwork by @allison_horst.)

Figure 9.1: Meet the penguins of the Palmer Archipelago, Antarctica. (Artwork by @allison_horst.)

9.2 Essentials of ggplot2

An obstacle to many technologies is that insiders tend to converse in special terms that appear to obscure rather than reveal insight. In this respect, ggplot2 is no exception. Fortunately, the number of needed terms is limited and the investment is worthwhile.

Before we can plot our first visualizations, we inspect the layered structure of visualizations created by ggplot2, introduce a minimal code template for ggplot() commands, explain some related terminology, and explicate a requirement on the input data that defines our plot.

9.2.1 The structure of ggplot2 plots

Figure 9.2 illustrates the layered structure of plots created by ggplot2:

The layered structure of plots in ggplot2.

Figure 9.2: The layered structure of plots in ggplot2.

Many terms of Figure 9.2 will initially seem a bit strange and technical. At this point, we only need to realize that every visualization (e.g., a bar chart) is based on data, which is transformed in some way (e.g., summarized) and represented by geometric objects (e.g., shapes) with aesthetic features (colors or sizes) and explained by additional text elements (e.g., labels and titles). In ggplot2, we can think of a visualization as the combination of multiple layers. As each layer identifies a key ingredient of visualizations, the rules for their combination provides a general language for creating visualizations.
To create a particular plot, we must learn to specify the details — or rely on the default values — of each layer.

9.2.2 A minimal template

Generally speaking, a plot takes some <DATA> as input and creates a visualization by mapping data variables or values to (parts of) geometric objects.

A minimal template of a ggplot() command can be reduced to the following structure:

# Minimal ggplot template:
ggplot(<DATA>) +             # 1. specify data set to use
  <GEOM_fun>(aes(<MAPPING>)  # 2. specify geom + variable mapping(s) 

The minimal template includes the following elements:

  • The <DATA> is a data frame or tibble that contains all data that is to be plotted and is shaped in suitable form (see below).
    Its variable names are the levers by which the data values are being mapped to the plot.

  • <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 two entities. Here, the mapping specifies the correspondence of variables to graphical elements, i.e., what goes where.

  • A geom’s visual appearance is controlled by aesthetics (e.g., colors, shapes, sizes, …) and can be customized by keyword arguments (e.g., color, fill, shape, size…). There are two general ways and positions to do this:

    1. within the aesthetic mapping (when varying visual features as a function of data properties), or
    2. by setting its arguments to specific values in <arg_1 = val_1, ..., arg_n = val_n> (when remaining constant).

Note that the functions that make up a ggplot() expression (which are typically positioned on separate lines) are connected by the + operator, rather than some other pipe operator.

9.2.3 Terminology

The two abstract notions that are most relevant in the context of the ggplot2 package are geoms and mapping.

Geometric objects

Basic types of visualizations in ggplot2 involve geometric objects (so-called geoms), which are accessed via dedicated functions (<GEOM_fun>). When viewing ggplot2 as a language for creating visualizations, geoms provide our main vocabulary (e.g., the concepts that need to be linked to create well-formed sentences). Thus, when first encountering ggplot2, it makes sense to familiarize ourselves with some basic geom functions that create key types of visualizations. Just like other R functions, geoms require specific input arguments to work. As we get more experienced, we will realize that geoms can be combined to create more complex plots and can invoke particular computations (so-called stats).

Mapping data to visual elements

When creating visualizations, the main regularity that beginners tend to struggle with is to define the mapping between data and elements of the visualization. The notion of a mapping is a relational concept that essentially specifies what goes where. The what part typically refers to some part of the data (e.g., a variable), whereas the where part refers to some aspect or part of the visualization (e.g., an axis, geometric object, or aesthetic feature).

Beyond these basic concepts, additional terms that matter in the context of ggplot2 are layers, aesthetics, facets, stats, and themes. Rather than explicitly defining each of these concepts, we will learn to use them when we need them.

An important requirement of ggplot() is that the to-be-plotted data must be in the right format (i.e., shape). Whereas this requirement often remains implicit (when the data is provided by a textbook or tutorial), it often is the biggest hurdle for using ggplot2 for visualizing one’s own data.

Data format

The <DATA> provided to the data argument of the ggplot() function must be rectangular table (i.e., a data.frame or tibble). Beyond this data type, ggplot() assumes that the data is formatted in a specific ways (in so-called “long” format, using factor variables to describe measurement values). Essentially, this format ensures that some variables characterizes or describes the values of other variables. In most sciences, we can distinguish between control variables (e.g., a person’s age, education, gender, or income), independent variables (e.g., different experimental conditions or treatments), and dependent variables (e.g., some test or performance score). When these three types are represented as different variables (so that the values of each individual is stored in a row of data), the values of control and independent variables can be thought of as characterizing or describing the value of the dependent variable. Another way of viewing this is that the control and independent variables provide “handles” that allow to sort or group the values of the dependent variables.

At this point, we do not need to worry about this and just work with existing sets of data that happen to be in the right shape. (We will discuss corresponding data transformations in Chapter 14 on Tidying data.)

The data used in the subsequent examples is copied from the penguins object of the palmerpenguins package (Horst et al., 2022). We assign this data to an R object pg and inspect it:

# Data:
pg <- palmerpenguins::penguins

# Inspect data:
dim(pg)
#> [1] 344   8

# Compact structure:
str(pg)
#> tibble [344 × 8] (S3: tbl_df/tbl/data.frame)
#>  $ species          : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ...
#>  $ island           : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ...
#>  $ bill_length_mm   : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
#>  $ bill_depth_mm    : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
#>  $ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ...
#>  $ body_mass_g      : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ...
#>  $ sex              : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ...
#>  $ year             : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ...

# Print some cases:
set.seed(100)  # for reproducible randomness
s <- sample(1:nrow(pg), size = 10)
knitr::kable(pg[s, ], caption = "10 random cases (rows) of the `penguins` data.")
Table 9.1: 10 random cases (rows) of the penguins data.
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Gentoo Biscoe 45.2 15.8 215 5300 male 2008
Adelie Biscoe 45.6 20.3 191 4600 male 2009
Gentoo Biscoe 50.1 15.0 225 5000 male 2008
Adelie Torgersen NA NA NA NA NA 2007
Chinstrap Dream 49.7 18.6 195 3600 male 2008
Chinstrap Dream 49.8 17.3 198 3675 female 2009
Adelie Dream 40.3 18.5 196 4350 male 2008
Adelie Torgersen 38.9 17.8 181 3625 female 2007
Gentoo Biscoe 47.3 15.3 222 5250 male 2007
Chinstrap Dream 43.2 16.6 187 2900 female 2007

The table shows the names of the 8 variables in our pg data, which are rather self-explanatory. For instance, the levels of the factor variables species and island can be used to group the other values (e.g., measurements of penguin physiology). Note that each row of data refers to one observation of a penguin and the data contains some missing (NA) values on some variables.

Do not worry if some of these terms remain unclear at this point. The following sections will provide plenty of examples that — hopefully — further explain and illustrate their meaning.

9.2.4 Plotting distributions

In Chapter 8, we used histograms and the hist() function to visualize the distribution of variable values (see Section 8.2.1). The corresponding geom function in ggplot2 is geom_histogram(). The data to be used is pg and the only aesthetic mapping required for geom_histogram() is to specify a continuous variable whose values should be mapped to the \(x\)-axis. Let’s use the flipper_length_mm variable for this purpose and create our first visualization with ggplot() (Figure 9.3):

# Basic histogram: 
ggplot(data = pg) + 
  geom_histogram(mapping = aes(x = flipper_length_mm))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_bin()`).
A basic histogram showing a distribution of variable values (created by ggplot2).

Figure 9.3: A basic histogram showing a distribution of variable values (created by ggplot2).

Note that we succeeded in creating our first histogram in ggplot2. This visualization is rather basic, but includes the bars of a histogram on a grey background with white grid lines (its signature theme_grey() is based on Tufte, 2006) and two axes with appropriate labels. As with the hist() function (from the base R graphics package), the default behavior of the geom_histogram() function is to categorize the values of the specified variable in discrete bins and display the counts of values per bin (as a bar chart).

Note also that evaluating our ggplot() command printed a message and a warning. Whereas the warning is due to our flipper_length_mm variable containing 2 missing (NA) values, the message suggests that we could specify a numeric value to the bins or to the binwidth parameters to override the default setting of bins = 30.

Just as we can use a natural language to say the same thing in different ways, the grammar of graphics allow for considerable flexibility in creating the same visualization. For instance, we can omit argument names of R functions, as long as the arguments (here data and x) are unambiguous and can move aesthetic mappings to the first line of the ggplot() expression. As a consequence, the following variants all create the same visualization:

# Basic histogram variants: 

# A: explicit version:
ggplot(data = pg) + 
  geom_histogram(mapping = aes(x = flipper_length_mm), bins = 30) +
  theme_grey()

# B: short version:
ggplot(pg) + 
  geom_histogram(aes(flipper_length_mm))

# C: moving aesthetic mapping to the 1st line:
ggplot(pg, aes(flipper_length_mm)) + 
  geom_histogram(bins = 30)

Adding colors, layers, labels, and themes

Before discovering more features of ggplot2, we should learn to improve its default visualizations. The basic histogram of Figure 9.3 is informative, but can be embellished by adding colors, more informative text labels, and choosing a different theme. Colors that do not vary by a data variable can be set as constants (i.e., outside the aes() function) to color-related arguments of the current geom. For the bars of geom_histogram(), the color argument refers to the border of the bars, whereas the bars themselves are colored by a fill argument. We can set these arguments to any of the 657 named R colors, available by evaluating colors(). (More complex color settings involving data variables and color scales will be introduced below.) The best way to change default text labels is by using the labs() function, which allows setting a range of labels by intuitive argument names. A good visualization should usually have a descriptive title and informative labels for its x- and y-axes. The theme() function of ggplot2 allows re-defining almost any aesthetic aspect of a plot. Rather than specifying all of them manually, we can choose one of the theme_*() functions that come with ggplot2.

An improved version of Figure 9.3 can be created as follows (Figure 9.4):

# Adding colors, labels and themes:
ggplot(pg) + 
  geom_histogram(aes(x = flipper_length_mm), binwidth = 2, 
                 color = "grey20", fill = "deepskyblue") + 
  labs(title = "Distribution of penguin flipper lengths",   
       x = "Flipper length (in mm)", y = "Frequency") + 
  theme_classic()
A histogram showing a distribution of values (with colors, labels, and a theme).

Figure 9.4: A histogram showing a distribution of values (with colors, labels, and a theme).

The code for Figure 9.4 shows that ggplot() commands can be viewed as a sequence of sub-commands, joined by the + operator. A neat feature of ggplot2 is that plots can be stored as R objects and then modified later. For instance, the code of the previous chunk could be decomposed into two steps:

# Adding colors, labels and themes:
pg_1 <- ggplot(pg) + 
  geom_histogram(aes(x = flipper_length_mm), binwidth = 2, 
                 color = "grey20", fill = "deepskyblue")
# pg_1  # basic plot with default settings

pg_2 <- pg_1 + 
  labs(title = "Distribution of penguin flipper lengths",  
       x = "Flipper length (in mm)", y = "Frequency") + 
  theme_bw()  # choose a different theme
pg_2  # annotated plot (with labels and a modified theme)
(ref:fig-ggplot-hist-2b)

Figure 9.5: (ref:fig-ggplot-hist-2b)

When storing a plot as an R object, evaluating the object prints the plot to the visualization area of RStudio. Here, pg_1 provides the basic histogram (plus two color constants), and pg_2 adds text labels and changes the default plot theme. Given the vast range of possible modifications, the best practice and strategy for working with ggplot2 is to first get the basic mechanics of the plot right (i.e., by adjusting geoms and variable mappings) before adding further bells and whistles for creating a more appealing visualization (e.g., by selecting aesthetics, text labels, or themes). The modular structure of ggplot2 objects supports this strategy.

Multiple layers (by adding geoms)

A powerful feature of ggplot2 is that visualizations can contain multiple layers. The notion of layers echoes the composition of complex plots when using base R graphics (see Section 8.3). Theoretically, each layer could use its own variable mappings and aesthetics, but most multi-layered plots employ different geoms, but share some mappings and use compatible aesthetics.

Which other geom fits to an existing plot depends on (a) the \(x\)- and \(y\)-axis mapping of an existing plot, and (b) the message to be expressed by adding another geom. Hence, it usually makes sense to select a primary geom and then check whether adding others improves a visualization. As we have only used ggplot2 to drawn a histogram so far, we can ask:

  • Which object (and corresponding geom) would add useful information to a histogram?

As the histogram visualizes the distribution of a variable’s values, we may want to add some measure of central tendency (e.g., a mean or median) or variability (e.g., standard deviation or error) to a plot. A geom that would allow us to do this would be geom_vline() which draws a vertical line by specifying a constant value for xintercept (and allows for aesthetic settings that change the color, linewidth and linetype of the line):

# Compute the mean flipper length:
mn_flip_len <- mean(pg$flipper_length_mm, na.rm = TRUE)

# Add a vertical line (as a 2nd geom) to a histogram:
ggplot(pg) + 
  # Geoms:
  geom_histogram(aes(x = flipper_length_mm), binwidth = 2,  
                 color = "grey20", fill = "deepskyblue") + # primary geom  
  geom_vline(xintercept = mn_flip_len, 
             color = "deeppink", linewidth = 1) +          # 2nd geom
  # Labels and themes: 
  labs(title = "Distribution of penguin flipper lengths",  
       x = "Flipper length (in mm)", y = "Frequency") + 
  theme_bw()  # choose a different theme

Note that our geom_vline() used only constant values, hence required no variable mappings in its aes() argument. Also, additional geom functions are entered as a new line of code by adding the + symbol (rather than the pipe) at the end of each function. Note also that we chose to compute the mean value mn_flip_len before and outside of our ggplot() function (and since the flipper_length_mm vector in pg contains missing values, we included na.rm = TRUE to ignore missing values). But we could also compute the mean value of pg$flipper_length_mm inside the ggplot() function:

# Compute mean add it as a vertical line (as a 2nd geom) to a histogram:
ggplot(pg) + 
  # Geoms:
  geom_histogram(aes(x = flipper_length_mm), binwidth = 2,  
                 color = "grey20", fill = "deepskyblue") + # primary geom  
  geom_vline(xintercept = mean(pg$flipper_length_mm, na.rm = TRUE), 
             color = "deeppink", linewidth = 1) +          # 2nd geom
  # Labels and themes: 
  labs(title = "Distribution of penguin flipper lengths",  
       x = "Flipper length (in mm)", y = "Frequency") + 
  theme_bw()  # choose a different theme

And as we have stored our earlier histogram as pg_2 (above), we could have added the layer that plots the vertical line as follows:

pg_2 + 
  geom_vline(xintercept = mean(pg$flipper_length_mm, na.rm = TRUE), 
             color = "deeppink", linewidth = 1)

When using more than one geom, the order of geoms matters insofar as later layers (or geoms) are added on top of earlier ones.

Irrespective of how we choose to draw the vertical line, the bi-modal distribution of the flipper length values seems rather ill-described by a single mean value. One way of further exploring the distribution lies in asking:

  • Do all kinds of penguins have the same distribution of values, or do different species have different distributions?

Grouping observations by mapping variables to aesthetics

Noting that “colors that do not vary by a data variable can be set as constants” (in the previous section) raises the question what other functions colors could serve. A prominent function lies in distinguishing between different groups of observations. This would require that a color element of our visualization is mapped to the levels of a categorical variable or factor (i.e., a variable for which we only care about class membership or whether any two observations have the same vs. different values). We can easily add this by moving a color argument into the aesthetic mapping function aes() and also assigning it to a categorical variable of our data. For instance, the following code maps the factor variable species to the fill color of the histogram bars (Figure 9.6):

# Grouping by mapping aesthetics (fill color) to a data variable (species):
pg_3 <- ggplot(pg) +
  geom_histogram(aes(x = flipper_length_mm, fill = species), binwidth = 2, 
                 color = "white", linewidth = .50)
pg_3
A histogram showing a distribution of values and color-coding a categorical variable.

Figure 9.6: A histogram showing a distribution of values and color-coding a categorical variable.

Note that moving fill = species into the aes() function had two effects: First, the counts of observations (penguins) that were expressed in the bars are now separated and color-coded for the three different species of penguins. Importantly, the different types of bars are stacked on top of each other, rather than positioned besides each other. Hence, the absolute height of the bars (on the y-axis) represents the counts of one, two, or three species based on their flipper_length_mm (on the x-axis).

Note also that the colors for the three different species were automatically chosen. We will learn how to select specific color scales in a moment, but note how our aesthetic mapping of a variable to the geom’s fill aesthetic differs from the constant color = "white" setting, which was specified outside the scope of the aes() function.) Additionally, a legend that describes the mapping of colors to species appeared to the right of the plotting area. This is very useful default behavior, but we may want to adjust the aesthetic properties (e.g., the fill color) to custom colors.

Changing color scales

When using ggplot2 without any additional specifications, the ggplot() function uses default colors. Depending on the categorical or continuous nature of the data variables that are being plotted, this can involve various color palettes. The ggplot2 term for a color palette is “scale” and some elements require distinguishing between their “color” and their “fill” color.

Deviating from the default colors usually requires mapping a data variable to the “color” or “fill” aesthetic and specifying a corresponding color scale. The range of color scale functions and corresponding palettes can be confusing and usually requires a lookup of the scale_color_* function.

A popular option is to use one of the palettes of the RColorBrewer package (Neuwirth, 2022) that come pre-packaged with ggplot2. The Brewer scales provide sequential, diverging and qualitative color palettes (see https://colorbrewer2.org for more information). Looking up ?scale_color_brewer reveals that its qualitative scales are labeled as “Accent”, “Dark2”, “Paired”, “Pastel1”, “Pastel2”, “Set1”, “Set2”, and “Set3”. As we aim to change the fill colors, we can select the corresponding palettes by specifying scale_fill_brewer(), e.g.,

# Grouping by aesthetics (and using a different color scale):
ggplot(pg) +
  geom_histogram(aes(x = flipper_length_mm, fill = species), binwidth = 2, 
                 color = "white", linewidth = .50) + 
  scale_fill_brewer(palette = "Set1")

When aiming to create a range of visualizations in a uniform style, it is advisable to define one or more palettes of custom colors. There are many R functions and packages supporting this task. For instance, we can use the unikn package, as it combines pleasing colors with useful color functions:

library(unikn)  # for colors and color functions

# seecol(pal_unikn_pref)  # view a (categorical) color palette

# A: Using unikn colors:
my_cols <- usecol(pal = c(Seeblau, Pinky, Seegruen), alpha = .67)  # 3 specific colors
my_cols <- usecol(pal = pal_unikn_pref, alpha = .67)               # a color palette

We will discuss colors and color functions in detail in the next Chapter on using colors in R (Chapter 10). At this point, we simply use the usecol() function to create a semi-transparent palette of three named colors (inspired by Figure 9.1 above) as follows:

# B: Using the penguin species colors (from Figure 9.1):
my_cols <- usecol(pal = c("orange", "orchid3", "turquoise4"), alpha = .67)

The usecol() function allows defining a color palette (of a variable length n ) and add transparency (by setting the alpha parameter to a value from 0 to 1). Using color transparency is a primary way to prevent overplotting (see Chapters 8 on Visualize in R and Chapter 10 on Using colors for more details and examples).

As we saved our plot as pg_3 above, we can add labels, apply our new custom color palette, and change the default theme as follows (Figure 9.7):

# Adding labels, color scale, and theme (to an existing plot):
pg_4 <- pg_3 +
  labs(title = "Distribution of penguin flipper lengths (by species)", 
       x = "Flipper length (in mm)", y = "Frequency", fill = "Species:") + 
  scale_fill_manual(values = my_cols) + 
  theme_unikn()
pg_4
A labeled and themed histogram showing a distribution of values and color-coding a categorical variable.

Figure 9.7: A labeled and themed histogram showing a distribution of values and color-coding a categorical variable.

Figure 9.7 is essentially a fancier version of Figure 9.6. But a good strategy when working with ggplot is to always create a basic plot first (by specifying the data, appropriate geoms, and variable mappings) before tweaking the plot further (by choosing colors, adding labels, or a theme).

Histograms are not the only way to transport information about the distribution of values. Later in this chapter, we will encounter geom_violin() and geom_rug() that also signal distributions. But before we explore additional geoms, we can practice what we have learned about ggplot2 so far.

Practice

Here are some practice tasks for plotting distributions:

  1. Playing with parameters: Re-create the basic histogram of Figure 9.3 and vary the bins or binwidth parameters.

    • What happens to the values on the \(y\)-axis when varying the parameters and why?
    • What happens when we change the variable mapping from x to y?
    • Which binwidth parameter corresponds to a value of bins = 30?
  2. Multiple layers: Show that the order of layers matters by plotting a variable’s mean value (as a vertical line) before showing its distribution (as a histogram).

  3. Multiple data arguments: When composing visualizations out of multiple layers (and geoms), we can pre-compute summary data and provide this data to additional geoms. The following code illustrates how we could pre-compute some values that are mapped to a 2nd layer of a plot. Evaluate and explain how this is done. Specifically,

  • What exactly does the plot show?
  • Which geom uses which data and variable mappings?
  • Why are there two color scale arguments?
  • Why is there only one color legend?
# Compute summary data:
means_by_species <- aggregate(flipper_length_mm ~ species, data = pg, FUN = mean)
means_by_species

ggplot(pg) +
  # Geoms:
  geom_histogram(mapping = aes(x = flipper_length_mm, fill = species), 
                 binwidth = 2, color = "white", linewidth = .50) + 
  geom_vline(data = means_by_species, 
             mapping = aes(xintercept = flipper_length_mm, color = species), 
             linewidth = 1, linetype = 2) + 
  # Labels and aesthetics:
  labs(title = "Distribution of penguin flipper lengths (by species)", 
       x = "Flipper length (in mm)", y = "Frequency", 
       color = "Species:", fill = "Species:") + 
  scale_fill_manual(values = my_cols) + 
  scale_color_manual(values = my_cols) + 
  theme_unikn()
  1. Alternative distributions: Study the documentation to geom_histogram() and explore its alternatives geom_density() and geom_freqpoly().

    • Create a histogram, density plot, and frequency polygon to show the distribution of body mass (for the 3 species of penguins).
    • What does the \(y\)-axis of a density plot show?
    • Which 2 of the 3 geoms can be combined with each other? Why not the 3rd?

Hint: It seems that geom_histogram() and geom_freqpoly() use a common scale of \(y\)-values. However, note that their following combination yields a peculiar error:

# Due to the different y-scales, geom_density() cannot be combined with the others.
# But geom_histogram() and geom_freqpoly() share the same scale:
ggplot(pg) + 
  geom_histogram(aes(x = body_mass_g), fill = "gold") + 
  geom_freqpoly(aes(x = body_mass_g), color = "steelblue", linewidth = 1) + 
  scale_fill_manual(values = my_cols) + 
  theme_unikn()

# However, for grouped values, we obtain:
ggplot(pg) + 
  geom_histogram(aes(x = body_mass_g, fill = species), binwidth = 150) + 
  geom_freqpoly(aes(x = body_mass_g, color = species), binwidth = 150, size = 2) + 
  scale_color_manual(values = my_cols) + 
  scale_fill_manual(values = my_cols) + 
  theme_unikn()

To fix this, study the documentation of geom_histogram() and then adjust its position argument.

9.2.5 Plotting summaries

In addition to plotting distributions, a common type of visualization aims to show a summary of one or more variables. While there are many ways of doing this, we will focus on bar charts and box plots.

Bar charts

A bar chart seems simple, but is actually a quite complicated plot. To realize this, we use a ggplot() expression for our pg data and geom_bar(), mapping the factor variable species to its \(x\)-axis (Figure 9.8):

# A basic bar chart: Showing counts:
ggplot(pg) +
  geom_bar(aes(x = species))
A basic bar chart (showing counts of cases).

Figure 9.8: A basic bar chart (showing counts of cases).

Figure 9.8 illustrates that geom_bar() does not simply plot given data values, but instead performs some computation. In ggplot2, geoms that compute stuff are linked to so-called stat (for statistics). By default, geom_bar groups observations into the categories specified by the variable levels mapped to x and then counts the number of cases per category. The following expression is a more explicit version of the previous code chunk (and would create the exact same plot as Figure 9.8):

# Explicate stat:
ggplot(pg) +
  geom_bar(aes(x = species), stat = "count")

The relation between geoms and stats

We have seen that geom_histogram() categorized observations in our data into groups (bins) and counted their frequency (Figure 9.3). Similarly, geom_bar() automatically counted the observations in the levels of a variable mapped to x (Figure 9.8).

This illustrates a hidden complexity in creating visualizations: Many types of visualizations require computations or transformations of the input data. If we provide raw data values to a ggplot2() command, the geoms aim to guess which transformation we desire by linking geoms to stat options (and corresponding functions).

The details of possible relations between geoms and stat options are difficult to understand. Rather than aiming to explain them here, we can only emphasize that geoms that compute values are linked to statistical functions that can also be invoked directly. When asking for ggplot2 advice online, experts often provide nifty solutions that perform quite complicated data transformations in variable mappings. Here are some examples that are — spoiler alert — likely to confuse you:

  • We can instruct ggplot2 to count observations by mapping ..count.. to a variable:
# Compute counts (in y mapping):
ggplot(pg) +
  geom_bar(aes(x = species, y = ..count..))
A bar chart counting the number of penguin observations by species.

Figure 9.9: A bar chart counting the number of penguin observations by species.

  • Instead of assigning y to ..count.., we can also ask for proportions (but then also need to specify the group level):
# Compute proportions (in y and group mapping):
ggplot(pg) +
  geom_bar(aes(x = species, y = ..prop.., group = 1))
A bar chart computing proportions of penguin species.

Figure 9.10: A bar chart computing proportions of penguin species.

  • In case this cryptic code does not suffice to confuse you, we can even omit the geom_ function altogether and directly ask for the summary of a given variable mapping (and specify the geom as an argument of the stat_summary() function):
# Compute a bar chart of means (by using stat_summary):
ggplot(pg, aes(x = species, y = body_mass_g)) +
    stat_summary(fun = mean, geom = "bar")
A bar chart computing penguin’s mean body mass by species from data (without an explicit geom function).

Figure 9.11: A bar chart computing penguin’s mean body mass by species from data (without an explicit geom function).

Do not worry if the last three examples remain rather confusing at this point! They are shown here only to illustrate the intimate connection between data visualization and data transformation. Actually, computing values from data in visualization commands may be convenient and powerful, but is also error-prone and intransparent. A better way of creating visualizations is to first compute all values that we are interested in (e.g., some measures of central tendency and variability) and then visualize these values. We will reconsider this issue below (in Section 9.3.2).

Fortunately, novice users of ggplot2 only need to know that some geoms provide stat options and choose an appropriate one (e.g., "count" vs. "identity") if the default option fails.25

Box plots

When aiming to visualize summary information of a continuous variable by the levels of some categorical variable, a good alternative is provided by a box plot. A box plot (or boxplot) compactly displays the mean tendency and distribution for all levels of a continuous variable. More specifically, it visualizes five summary statistics: The median (as a horizontal line), two hinges (indicating the value range’s 25th and 75th percentiles), and two whiskers (marking $$1.5 of the inter-quartile range, IQR). Additionally, any outliers beyond this range are shown (as points beyond the end of the whiskers).

To create a boxplot in ggplot2, we use geom_boxplot() and map a categorical variable to x and a continuous variable to y. Figure 9.12 uses the pg data to illustrate penguin body mass (i.e., the variable body_mass_g) by species:

ggplot(pg) + 
  geom_boxplot(aes(x = species, y = body_mass_g), fill = "gold")
A box plot showing penguin’s body mass by species (with additional range information).

Figure 9.12: A box plot showing penguin’s body mass by species (with additional range information).

In the basic box plot of Figure 9.12, the fill aesthetic was set to a constant (e.g., the color name "gold"). Hence, the 50%-range of values within the hinges were drawn in this color. But as we distinguished penguins by species (in our mapping to x), the fill color could also be mapped to the species variable. Figure 9.13 does this, and uses the manual color choices (from above), as well as adding text labels and a theme:

ggplot(pg) +
  geom_boxplot(aes(x = species, y = body_mass_g, fill = species)) + 
  scale_fill_manual(values = my_cols) + 
  labs(title = "Penguin mass by species", 
       x = "Species", y = "Mean mass (in g)", fill = "Species") +  
  theme_grau()
A box plot showing penguin’s body mass by species (with labels and aesthetic tweaks).

Figure 9.13: A box plot showing penguin’s body mass by species (with labels and aesthetic tweaks).

Overall, investing into manual data transformation and computations adds control and transparency to our visualizations and simplifies the code. As an example, we have shown that bar charts showing means of some variable can be created by using geom_col() rather than by using geom_bar(). However, when transforming data to be plotted we must make sure that the data supplied as input to gglot() contains all the variables and values that we want to visualize.

Better bar plots are often column plots: Pre-compute the values to display. If we had pre-computed the counts, we could map them to y and specify stat = "identity".

A good alternative to many bar charts — if they provide mean information — is provided by box plots.

Practice

Here are some practice tasks on plotting summaries in bar charts or box plots:

  1. Understanding geoms: Using the summary table tb, explain the result of the following command:
# Create summary data (as tb):
tb <- pg %>%
  group_by(species) %>%
  summarise(mn_flip_len  = mean(flipper_length_mm, na.rm = TRUE))
tb
#> # A tibble: 3 × 2
#>   species   mn_flip_len
#>   <fct>           <dbl>
#> 1 Adelie           190.
#> 2 Chinstrap        196.
#> 3 Gentoo           217.

# Plot:
ggplot(tb) +
  geom_bar(aes(x = species))
- How could we fix this plot (to show the average flipper length by species)?
  1. Flipping coordinates:
    • Evaluate the following expression and explain its result:
ggplot(pg) +
  geom_bar(aes(x = species)) + 
  coord_flip()
- How can an identical plot be created without using `coord_flip()`?
  1. Simple bar charts: Create a bar plot for the pg data showing the counts of penguins observed on each island.

  2. Misleading settings: Explain the output of the following command and find a better solution.

    • Why is it misleading?
    • How could it be fixed?
# Adding a factor variable:
ggplot(pg, aes(x = species, y = body_mass_g, fill = sex)) +
    stat_summary(fun = mean, na.rm = TRUE, geom = "bar", position = "stack")
  1. Create a box plot that shows the mean flipper length of penguins on each of the three islands.

    • Add aes(fill = island)) to geom_boxplot() and interpret the result.
    • Change the fill aesthetic of geom_boxplot() to aes(fill = species)) and explain the result.
ggplot(pg) + 
  geom_boxplot(aes(x = island, y = body_mass_g, fill = island))

# same as:
ggplot(pg, aes(x = island, y = body_mass_g)) + 
  geom_boxplot(aes(fill = island))

# Fill color by species:
ggplot(pg) + 
  geom_boxplot(aes(x = island, y = body_mass_g, fill = species))
  1. Box plot with multiple mappings:
    • Interpret and explain the result of the following expression:
# Fill color by island: 
ggplot(pg) + 
  geom_boxplot(aes(x = species, y = body_mass_g, fill = island))

9.2.6 Plotting relations

Another common type of plot visualizes the relationship between two or more variables. Important types of plots that do this include scatterplots and visualizations of lines or trends. This section will introduce corresponding ggplot2 geoms.

Scatterplots

Scatterplots visualize the relation between two variables for a number of observations by corresponding points that are located in 2-dimensional space. Assuming two orthogonal axes (typically \(x\)- and \(y\)-axes), a primary variable is mapped to the \(x\)-axis, and a secondary variable is mapped to the \(y\)-axis of the plot. The points representing the individual observations then show the value of \(y\) as a function of \(x\).26

As an example of a simple scatterplot, we aim to solve the following task:

  • Visualize the relationship between body mass and flipper length for (the 3 species of) penguins.

Solving this task in ggplot2 is simple and straightforward. We provide our pg data to ggplot() and select the geometric object geom_point() with the aesthetic mappings x = body_mass_g and y = flipper_length_mm (Figure 9.14):

ggplot(pg) +
  geom_point(aes(x = body_mass_g, y = flipper_length_mm))
A basic scatterplot using geom_point(), but suffering from overplotting.

Figure 9.14: A basic scatterplot using geom_point(), but suffering from overplotting.

Overall, this basic scatterplot suggests a positive and possibly linear correlation between penguin’s body mass (mapped to the values on the \(x\)-axis) and their flipper length (mapped to the values of the \(y\)-axis). However, the example also illustrate a typical problem of scatterplots: When many points are clustered near each other or even at the same locations, they overlap or obscure each other — a phenomenon known as overplotting. There are many ways of preventing overplotting in ggplot2. In the context of scatterplots, a popular strategy against overplotting consists in using colors, color transparency, or grouping points into clusters by changing their aesthetic features.

The aesthetic features of points include colors, sizes, and symbol shapes. As we have seen for other geoms, we can map either constant values or variables to aesthetic features of geom_point() (Figure 9.15):

sp_01 <- ggplot(pg) +
  geom_point(aes(x = body_mass_g, y = flipper_length_mm,  # essential mappings 
                 col = species, shape = species           # aesthetic variables
                 ),                                       # vs. 
             alpha = .50, size = 2                        # aesthetic constants
             )
sp_01
A scatterplot using geom_point() and an aesthetic grouping variable (species).

Figure 9.15: A scatterplot using geom_point() and an aesthetic grouping variable (species).

Note that Figure 9.15 mapped two aesthetic features (col and shape) to a variable (species), whereas two others (alpha and size) were mapped to constant values. The effect of this difference is that the species variable is used to group the geom’s visual elements (i.e., varying point color and shape by the different types of species), whereas their color transparency and size is set to constant values.

Finally, we can further improve our previous plot by choosing custom colors, text labels, and choosing another theme. Since Figure 9.15 was saved as an R object (sp_01), we can adjust the previous plot by adding labels, color scales, and theme functions (Figure 9.16):

sp_01 + 
  labs(title = "Penguin's flipper length by body mass (by species)", 
       x = "Body mass (in g)", y = "Flipper length (in mm)", 
       col = "Species:", shape = "Species:") + 
  scale_color_manual(values = my_cols) + 
  theme_bw()
Adjusting our scatterplot’s text labels, color scale, and theme.

Figure 9.16: Adjusting our scatterplot’s text labels, color scale, and theme.

As before, tweaking aesthetics and adding text labels to the initial plot improved our visualization by making it both prettier and easier to interpret. (We will later see that faceting — i.e., splitting a plot into several sub-plots — is another way of preventing overplotting in ggplot.)

1. Curves and mathematical functions

We first consider curves expressing mathematical functions:

Plotting straight lines (or linear functions) is straightforward with geoms for horizontal (geom_hline), vertical (geom_vline), or any linear line (geom_abline) and corresponding arguments (yintercept, xintercept, or intercept and slope, respectively). The hardest part here is to provide some data and an appropriate aesthetic mapping. In the following, we provide a minimal data frame (only containing a variable x with a single value of 0) and the mapping x = x:

# Plotting straight lines:
ggplot(data = data.frame(x = 0), aes(x = x)) +
  geom_hline(yintercept = -2, color = Seeblau, linewidth = 1) + 
  geom_vline(xintercept =  4, color = Seegruen, linewidth = 1, linetype = 2) + 
  geom_abline(intercept = -1, slope = 1, color = Pinky, linewidth = 1, linetype = 4) +
  # Set axis limits (and types):
  scale_x_continuous(limits = c(-10, 10)) + 
  scale_y_continuous(limits = c(-10, 10))

Note that we explicitly defined the limits of both axes by scale_ functions. Otherwise, ggplot() would have chosen an automatic range.

Beyond plotting straight lines, we can use geom_function() for plotting statistical or any arbitrary function. To do so, the data to be plotted by the ggplot() function should specify the range of x (e.g., as a data frame containing the minimum and maximum values of the to-be-plotted range) and the aesthetic mapping should indicate aes(x = x). (We could also plot functions without providing any data, but then need to specify the axis range, e.g., by xlim(-10, 10).)

We first demonstrate geom_function() for a statistical function. As the R language originated in a statistics context, its native stats package provides many useful functions. For instance, the density of a normal distribution is provided by the dnorm() function, which takes two arguments (mean and sd):

# Statistical function / Density of normal distribution:
sf_1 <- ggplot(data.frame(x = c(0, 1)), aes(x = x)) + 
               geom_function(fun = dnorm, args = list(.50, .15), 
                             aes(color = "Function 1"), linewidth = 1)
sf_1

As before, we can improve our function plot by adding more function curves, change the x-axis, or edit text labels, colors, or the plot theme:

# Statistical functions:
sf_1 + 
  geom_function(fun = dnorm, args = list(.60, .10),
                aes(color = "Function 2"), linetype = 2, linewidth = 1) +
  # Change scale, labels, colors, and theme: 
  scale_x_continuous(name = "Probability", breaks = seq(0, 1, .20), limits = c(0, 1)) +
  labs(title = "Two normal density curves", 
       y = "Frequency", color = "Normal curves:") +
  scale_color_manual(values = my_cols) +
  theme_unikn()

Beyond plotting pre-defined functions, we can define and plot any arbitrary function. As we have seen in Chapter 5 on Functions, we can easily define our own functions (as my_fun <- function(){}). We can then visualize it by ggplot2 by using geom_function():

or stat_function().

# Any function of x:
my_fun <- function(x){
  sin(x)
}

# Using geom_function(): 
ggplot(data.frame(x = c(0, 13)), aes(x = x)) +
  geom_function(fun = my_fun, color = Seeblau, linewidth = 1)

As geom_function() is linked to stat_function(), the last ggplot() expression is identical to:

# Using stat_function():
ggplot(data.frame(x = c(0, 13)), aes(x = x)) +
  stat_function(fun = my_fun, color = Seeblau, linewidth = 1)

If a user-defined function contains additional arguments, these can be supplied as a list of args to geom_function() or stat_function():

# A function of x with 2 additional arguments:
my_fun <- function(x, shift, fac){
  sin(x - shift) * fac  
}

# Using geom_function(): 
ggplot(data.frame(x = c(0, 13)), aes(x = x)) +
  geom_function(fun = my_fun, args = list(1, 2), color = Seeblau, linewidth = 1) + 
  geom_function(fun = my_fun, args = list(3, 1), color =   Pinky, linewidth = 1) + 
  theme_minimal()

Again, the two instances of geom_function() in the last ggplot() call could be replaced by corresponding stat_function() calls:

# Using stat_function() to draw lines:
ggplot(data.frame(x = c(0, 13)), aes(x = x)) +
  stat_function(fun = my_fun, args = list(1, 2), color = Seeblau, linewidth = 1) + 
  stat_function(fun = my_fun, args = list(3, 1), color = Pinky,   linewidth = 1)  + 
  theme_minimal()

A neat feature of using stat_function() is that it is linked to geom_line() by default, but can flexibly be used with other geoms:

# Using stat_function() with various geoms:
ggplot(data.frame(x = c(0, 13)), aes(x = x)) +
  stat_function(fun = my_fun, args = list(1, 2), geom = "line", color = Seeblau, linewidth = 1) + 
  stat_function(fun = my_fun, args = list(3, 1), geom = "point", color = Pinky,  size = 1.5) +
  stat_function(fun = dnorm, args = list(7, 1),  geom = "polygon", color = Petrol, fill = "honeydew") +
  theme_minimal()

For a maximum of flexibility, we can even omit the initial data and aes() mapping, and define functions, their geom, range and aesthetics all inside of stat_function():

ggplot() +
  stat_function(fun = function(x, s, c){-(x - s)^2 + c}, args = list(5, 10), 
                xlim = c(-5, 10), color = Seeblau, linewidth = 1) + 
  stat_function(fun = function(x, a, b){a * x + b}, args = list(5, -50), 
                xlim = c(0, 15), geom = "point", color = Pinky, shape = 21)  + 
  theme_minimal()

When lines are not defined by mathematical functions, we typically have some data that expresses developments or trends.

2. Line plots

Plotting lines: Link values given in data (geom_path())

Line plot can show developments or relations: Trends over time or some other variable.

The penguins data is probably not the most suitable data for asking developmental questions: It only contains observations from three years and its measures of penguin physiology are unlikely to show large changes in that time span. Nevertheless, we can use it to visualize penguin flipper length over the observed three years. Using our pg version of the data, we first compute a quick summary table that provides the mean flipper length by species and year. (We do so using a dplyr pipe, which we will discuss in Chapter 13 on Transforming data.)

# Data: 
# pg

# Create some time-based summary: 
# Penguin's measurements by species x year)
tb <- pg %>%
  group_by(species, year) %>%
  summarise(nr = n(),
            # nr_na = sum(is.na(flipper_length_mm)), 
            # mn_body_mass = mean(body_mass_g, na.rm = TRUE),
            mn_flip_len  = mean(flipper_length_mm, na.rm = TRUE))

# Print tb:
knitr::kable(tb, 
             caption = "Mean flipper length of penguins by `species` and `year`.", 
             digits = 1)
Table 9.2: Mean flipper length of penguins by species and year.
species year nr mn_flip_len
Adelie 2007 50 186.6
Adelie 2008 50 191.0
Adelie 2009 52 192.1
Chinstrap 2007 26 192.4
Chinstrap 2008 18 197.7
Chinstrap 2009 24 198.1
Gentoo 2007 34 215.1
Gentoo 2008 46 217.6
Gentoo 2009 44 218.4

As our summary table tb is in “long” format (i.e., contains our variable of interest mn_flip_len as a function of two other variables species and year), we use it as input to a ggplot() expression. (Note that tb is a much more compact table than pg.) As we want our lines to vary by year and species, we map year to x and use species as a group factor in geom_line().
To further distinguish our lines, we also map species to color and use the same data with geom_point() that additionally maps species to the shape:

lp_1 <- ggplot(data = tb) + 
  geom_line(aes(x = year, y = mn_flip_len, group = species, color = species), linewidth = 1.5) +
  geom_point(aes(x = year, y = mn_flip_len, color = species, shape = species), size = 3) 
lp_1

The resulting line plot shows three lines (with different colors and point shapes) for the three species, and even suggests that there may be some increase in the mean flipper length over the three years. However, when noting ggplot2’s automatic choice of axis scales, we realize that the magnitude of these changes may be a bit misleading (especially due to truncating the range of \(y\)-axis values). We therefore adjust our initial line plot lp_1 to a sensible axis values, and add some labels, scales, and another theme (Figure 9.17):

# Adjusting axes and tweaking aesthetics:
lp_1 + 
  # Adjust labels, scales, and theme:
  labs(title = "Mean penguin flipper length by species over 3 years",  
       x = "Year", y = "Mean flipper length (mm)", color = "Species:", shape = "Species:") + 
  scale_x_continuous(limits = c(2007, 2009), breaks = c(2007, 2008, 2009)) +   
  scale_y_continuous(limits = c(0, 220)) + 
  scale_color_manual(values = my_cols) +   
  theme_bw()
A line plot illustrating the mean flipper length of penguins observed in three years.

Figure 9.17: A line plot illustrating the mean flipper length of penguins observed in three years.

In Figure 9.17, the apparent increase in the mean flipper length values looks a lot less dramatic — illustrating that we should never trust plots with truncated axes and delegate judgments regarding differences to statistical analysis. And although Figure 9.17 provides a fine example of a line plot, using continuous lines suggests that we are observing the same penguins over time. If this is not the case, using some visualization with discrete elements (e.g., a bar or point chart) may be a better choice. (Note that Exercise 9.5.5 will create lines plots that depict larger changes over time.)

3. Trend lines

Summary trends show developments (over time or some other variable), but also average over some other variable. Trend lines can help judging the shape of relations (i.e., curvilinear, linear, quadratic?) or discovering patterns (e.g., clusters, trends).

Task: Plotting summary trends, which requires computing trends over some other data variable. Fortunately, geom_smooth() does the computation for us.

Figure 9.14 showed the relation between penguins’ body mass and flipper length as a scatterplot. Rather than showing the individual data points with geom_points(), we can use geom_smooth() to depict the average trend as a line (Figure 9.18):

ggplot(pg) +
  geom_smooth(aes(x = body_mass_g, y = flipper_length_mm)) + 
  labs(title = "Penguin flipper length by body mass (as curvilinear trend)")
Plotting the (curvilinear) trend between two variables by geom_smooth().

Figure 9.18: Plotting the (curvilinear) trend between two variables by geom_smooth().

Figure 9.18 illustrates the positive association between penguins’ body mass and flipper length as both a trend line with dispersion information (as a shaded area around the trend line). The trend computed by geom_smooth()’s default smoothing method (known as "loess" for fewer than 1,000 observations) appears somewhat curvilinear, but could well be approximated by a linear model when ignoring the sparser and more uncertain data at both extremes of the body mass range. Figure 9.19 shows this linear trend by specifying method = "lm" as an argument to geom_smooth():

ggplot(pg) +
  geom_smooth(aes(x = body_mass_g, y = flipper_length_mm), method = "lm") + 
  labs(title = "Penguin flipper length by body mass (as linear trend)")
Plotting the linear trend between two variables by geom_smooth().

Figure 9.19: Plotting the linear trend between two variables by geom_smooth().

Let’s add a grouping variable to further inspect trends:

In our section on scatterplots, Figure 9.15 used the aesthetic mapping color = species to group the points by species. We can now extend this logic to our trend, by adjusting the mapping of geom_smooth() in an analog fashion (Figure 9.20):

tp_02 <- ggplot(pg) +
  geom_smooth(aes(x = body_mass_g, y = flipper_length_mm, color = species)) + 
  labs(title = "Penguin flipper length by body mass for each species (as curvilinear trends)")
tp_02
Plotting (curvilinear) trends by geom_smooth() with an aesthetic grouping variable (species).

Figure 9.20: Plotting (curvilinear) trends by geom_smooth() with an aesthetic grouping variable (species).

Note that Figure 9.20 added geom_smooth() with analog mappings to Figure 9.15. In psychology, we are often interested in linear trends (or linear regression models). We can obtain this in geom_smooth() by adding method = "lm" (Figure 9.21):

tp_03 <- ggplot(pg) +
  geom_smooth(aes(x = body_mass_g, y = flipper_length_mm, col = species), method = "lm", alpha = .20) + 
  labs(title = "Penguin flipper length by body mass for each species (as linear trends)") 
tp_03
Plotting linear trends by geom_smooth() with an aesthetic grouping variable (species).

Figure 9.21: Plotting linear trends by geom_smooth() with an aesthetic grouping variable (species).

As before, we can further improve our plots by choosing better colors, labels, or themes. Again, since Figure 9.21 was saved as an R object (tp_03), we can adjust it by adding labels, color scales, and a theme (Figure 9.22):

tp_04 <- tp_03 +
  labs(title = "Penguin flipper length by body mass for each species (as linear trends)",  
       x = "Body mass (in g)", y = "Flipper length (in mm)", 
       col = "Species:", shape = "Species:") + 
  scale_color_manual(values = my_cols) + 
  theme_unikn()
tp_04
An adjusted version of linear trends by geom_smooth() with an aesthetic grouping variable (species).

Figure 9.22: An adjusted version of linear trends by geom_smooth() with an aesthetic grouping variable (species).

As we have seen, geom_smooth() provides flexible ways of depicting relationships between two continuous variables as trend lines. In practical applications, it will often make sense to combine scatterplots with trend lines (see the subsection Better relational plots of Section 9.3.2 below).

We conclude this section on line plots by some exercises that practice what we have learned.

Practice

Here are some practice tasks on plotting relationships in scatterplots, lines or trends:

  1. Bill relations: What is the relation between penguin’s bill length and bill depth?

    • Create a scatterplot to visualize the relationship between both variables for the pg data.
    • Does this relationship vary for different species of penguins?
  2. Scattered penguins: The following code builds on our previous scatterplot (Figure 9.15, saved above as sp_01), but maps the aesthetic feature shape to the data variable island, rather than to species.

    • Evaluate the code and the explain the resulting scatterplot.
    • Criticize the plot’s trade-offs: What is good or bad about it?
    • Try improving the plot so that the different types of species and island become more transparent.
ggplot(pg) +
  geom_point(aes(x = body_mass_g, y = flipper_length_mm, # essential mappings 
                 col = species, shape = island),         # aesthetic variables vs. 
             alpha = .50, size = 2)                      # aesthetic constants
# Possible solutions:

# Good: Mapping 2 variables means that there are many things to see
# Bad:  Complexity makes some things hard to see.

# Possible solutions:

# A: Tweaking aesthetics to improve visibility: ---- 
ggplot(pg) +
  geom_point(aes(x = body_mass_g, y = flipper_length_mm,  # essential mappings 
                 col = species, shape = island            # aesthetic variables
                 ),                                       # vs. 
             alpha = .40, size = 5                        # aesthetic constants
             ) +
  scale_color_manual(values = my_cols) + 
  theme_minimal()

# B: Using 3 facets: ---- 
ggplot(pg) +
  geom_point(aes(x = body_mass_g, y = flipper_length_mm,  # essential mappings 
                 col = species, shape = island            # aesthetic variables
                 ),                                       # vs. 
             alpha = .50, size = 2                        # aesthetic constants
             ) +
  facet_wrap(~island)

# C: Using 3 x 3 faceting: ----
ggplot(pg) +
  geom_point(aes(x = body_mass_g, y = flipper_length_mm,  # essential mappings 
                 col = species, shape = island            # aesthetic variables
                 ),                                       # vs. 
             alpha = .50, size = 2                        # aesthetic constants
             ) +
  facet_grid(species~island)
  1. Plotting mathematical functions: Figure 9.23 visualizes three mathematical functions.

    • Try re-creating each line using geom_function() (without restraining the range of \(x\)-values).
    • Try re-creating Figure 9.23 using stat_function() (with the same ranges of \(x\)-values).
Plotting three mathematical functions (i.e., two linear and one quadratic function).

Figure 9.23: Plotting three mathematical functions (i.e., two linear and one quadratic function).

  1. Penguin lines: Create a line plot that uses the pg data to show the development of penguin’s mean body mass by island over the observed period of three years. Note that the steps required for this task are analog to those leading to Figure 9.17 (above):

    • Create a small summary table that contains all desired variables.
    • Use this table to create a basic line plot.
    • Tweak the line plot (by adjusting its scales, labels, and theme) to provide a clear view of the “development” over time.
    • Turn your line plot into a bar plot.
  1. Bill trends: Add trend lines to your scatterplot showing the relation between penguin’s bill length and bill depth (from 1 above).

    • Add trend lines both to the overall scatterplot and to the version distinguishing three species.
    • Explore the effects of different method arguments.

Having learned to use ggplot2 to visualize distributions (e.g., by using geom_histogram() or geom_density()), summaries (geom_bar(), geom_col(), or geom_boxplot()) or relations as sets of points (geom_point()) or lines (geom_function(), geom_line(), geom_smooth()), we are ready to discover some of its more advanced aspects.

9.3 Advanced aspects of ggplot2

Using more advanced features of ggplot2 requires a more general template than the minimal one of Section 9.2.2 (above). In addition to aesthetic mappings and layers of geoms, we will encounter facets. Whereas layers denote multiple levels of geoms on a plot (behind/before each other),
facets create multiple variants of a plot (beside/next to each other).

The additional topics mentioned in this section are:

  • Creating better plots by combining geoms
  • Splitting up plots into facets
  • Adjusting axes and coordinate systems
  • Combining and saving plots

We will conclude this section by mentioning ggplot2 extensions.

9.3.1 Generic template

A generic template for creating a visualization in ggplot2 with some additional bells and whistles 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, ...) +     # - optional arguments to geom
  ...                                   # - additional geoms + mappings
  <FACET_fun> +                         # - optional facet function
  <LOOK_GOOD_fun>                       # - optional themes, colors, labels...

The generic template includes the following elements (beyond the <DATA> and <GEOM_fun> of the minimal template):

  • Multiple <GEOM_fun> yield layers of geometric elements.

  • An optional <FACET_fun> uses one or more variable(s) to split a complex plot into multiple sub-plots.

  • 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).

9.3.2 Better plots with ggplot2

We saw above that plots can be constructed out of multiple layers. The ability to combine geoms can be a powerful tool for creating better plots. When using multiple geoms (in layers):

  • We can specify common mappings globally, rather than locally.
  • We should consider the order of geoms: Later geoms appear on top of earlier geoms.

However, not every geom can be combined with every other.

Examples

Three examples of combining layers of geoms:

  1. Visualizing raw and aggregate data
  2. Better summary plots
  3. Better relational plots

Better raw data plots

Task: Combine raw data with distributions and summaries

Figure 9.13 (above) showed penguin’s body mass by species as a box plot. Figure 9.24 adds two more geoms:

raw_p_1 <- ggplot(pg) +
  geom_violin(aes(x = species, y = body_mass_g, fill = species)) + 
  geom_boxplot(aes(x = species, y = body_mass_g)) + 
  geom_jitter(aes(x = species, y = body_mass_g))
raw_p_1
A raw data plot that adds additional information to a box plot.

Figure 9.24: A raw data plot that adds additional information to a box plot.

As common aesthetic mappings can be abstracted by moving them into first line (as an argument of the initial ggplot() function), the following code would provide the same plot:

raw_p_2 <- ggplot(pg, aes(x = species, y = body_mass_g)) +
  geom_violin(aes(fill = species)) + 
  geom_boxplot() + 
  geom_jitter()
raw_p_2

Finally, we improve the plot further by some additional tweaks. Beyond adding text labels, choosing a customized color palette and a theme, Figure 9.25 adjusts the width of all three geoms to compatible values:

ggplot(pg, aes(x = species, y = body_mass_g)) +
  geom_violin(aes(fill = species), width = .50) + 
  geom_boxplot(width = .20) + 
  geom_jitter(width = .05, color = grey(0, .25)) +  
  scale_fill_manual(values = my_cols) + 
  labs(title = "Penguin mass by species", 
       x = "Species", y = "Mean mass (in g)", fill = "Species") +  
  theme_unikn()
The same raw data plot with manual colors, width parameters, and informative labels.

Figure 9.25: The same raw data plot with manual colors, width parameters, and informative labels.

Overall, Figure 9.25 provides detailed information on the central tendency and the value distribution of some variable of interest (i.e., penguin’s body mass) as a function of a categorical variable (species).

Better summary plots

Task: Combine bar or line plots with labels and error bars (and annotations):

Rather than relying on intransparent data transformations, a better way to create informative bar charts is to explicitly compute all values that we aim to visualize. This may require more effort, but also provides more control and is more transparent and reproducible.

As an example, we re-create the basic counts of Figure 9.8 and the mean chart of Figure 9.11 in a different way. Interestingly, doing so will not require geom_bar(), but rather geom_col().

  1. We first compute the counts of observations and means of the body_mass_g variable for the pg data:
library(tidyverse)

# Compute summaries for body_mass_g of penguins per species:
tb <- pg %>% 
  group_by(species) %>%
  summarise(n = n(),
            n_NA = sum(is.na(body_mass_g)), 
            mean_body_mass = mean(body_mass_g, na.rm = TRUE)
            )
tb
#> # A tibble: 3 × 4
#>   species       n  n_NA mean_body_mass
#>   <fct>     <int> <int>          <dbl>
#> 1 Adelie      152     1          3701.
#> 2 Chinstrap    68     0          3733.
#> 3 Gentoo      124     1          5076.

# Print tb: 
knitr::kable(tb, caption = "A table of computed summary values.", digits = 2)
Table 9.3: A table of computed summary values.
species n n_NA mean_body_mass
Adelie 152 1 3700.66
Chinstrap 68 0 3733.09
Gentoo 124 1 5076.02

Note that we used a dplyr pipe to group, count and summarize cases (see Chapter 13 on Transforming data), but could have used two base R functions to obtain the same values:

# In base R:
table(pg$species)  # N per species 
#> 
#>    Adelie Chinstrap    Gentoo 
#>       152        68       124
tapply(pg$body_mass_g, pg$species, mean, na.rm = TRUE)  # mean body_mass_g per species
#>    Adelie Chinstrap    Gentoo 
#>  3700.662  3733.088  5076.016

We now can easily re-create Figure 9.8. But as our summary table tb already contains the desired counts (in a variable named n), we no longer want geom_bar() to count anything. Thus, we could map the y variable of geom_bar() to n and switch off the geom’s default counting behavior (by specifying stat = "identity"):

ggplot(tb) +
  geom_bar(aes(x = species, y = n), stat = "identity")

An easier way to achieve the same is to replace geom_bar() by geom_col(), as the latter uses stat = "identity" by default:

ggplot(tb) +
  geom_col(aes(x = species, y = n))

Why is using geom_col() a better way of creating bar charts? The main reason is that the computation of the values in tb was entirely under our control and fully transparent. A related benefit is that we can easily re-create Figure 9.11 by only changing the variable mapping of y:

ggplot(tb) +
  geom_col(aes(x = species, y = mean_body_mass))

This code is arguably much simpler than the one that created Figure 9.11. Note also that the amount of data supplied to the corresponding ggplot() functions is vastly different: Whereas pg is a raw data table of 344 rows and 8 columns, tb is only a small summary table of 3 rows and 4 columns. However, smaller is not always better. If we ever wanted to visualize other aspects of the data, the data argument provided as input to the ggplot() function must provide corresponding variables. To illustrate this point, we will further refine the aesthetic settings of our original bar chart (showing the counts of penguins by species in Figure 9.8) by the sex variable. As we have dropped this variable from tb, we need to use the original pg data for this purpose (or alternatively include sex in tb).

Adjusting aesthetics

As we have seen above, we can easily adjust colors, text labels, and themes to create prettier and more informative bar charts:

ggplot(pg) +
  geom_bar(aes(x = species, fill = species), stat = "count") + 
  labs(title = "Number of penguins by species", 
       x = "Species", y = "Frequency", fill = "Species:") + 
  scale_fill_manual(values = my_cols) + 
  theme_unikn()

Rather than mapping x and fill to the same variable, we could set fill to a different variable of our pg data. For instance, let’s see what happens when we set fill to sex:

# Add sub-category (by mapping fill to sex):
ggplot(pg) +
  geom_bar(aes(x = species, fill = sex)) + 
  scale_fill_manual(values = my_cols) +
  theme_unikn()

By mapping another variable to the fill color of geom_bar() we revealed additional information about our data. Note also that the sub-categories of each bar are stacked. The reason is that the position argument of geom_bar() is set to "stack" by default. As the color mapping of the sex variable may be somewhat un-intuitive, we also create an alternative color vector:

# Choose 3 colors (to be mapped to sex):
my_3_cols <- my_cols[c(2, 1, 8)]

# Show sub-category (as stacked bars):
ggplot(pg) +
  geom_bar(aes(x = species, fill = sex), position = "stack") + 
  scale_fill_manual(values = my_3_cols) +
  theme_unikn()

An alternative position setting is "dodge":

# Show sub-category (as dodged bars):
ggplot(pg) +
  geom_bar(aes(x = species, fill = sex), position = "dodge") +
  scale_fill_manual(values = my_3_cols) + 
  theme_unikn()

However, if not all categories contain the same sub-categories (here sex values), the width of the bars may need further adjustments.

Better relational plots

Task: Combine scatterplots with (curvilinear or linear) trends and distribution info (rugs)

Figure 9.14 showed the relation between penguins’ body mass and flipper length as a scatterplot, whereas Figure 9.18 illustrated the same relation as a curvilinear trend. As each version has its unique strengths, it is straightforward to combine both layers. Figure 9.26 combines geom_smooth() with geom_point() to show a trend line with raw data information:

ggplot(pg) +
  geom_smooth(aes(x = body_mass_g, y = flipper_length_mm)) + 
  geom_point(aes(x = body_mass_g, y = flipper_length_mm)) + 
  labs(title = "Penguin flipper length by body mass (as scatterplot and curvilinear trend)")
Plotting the relation between two variables as a scatterplot with trend line.

Figure 9.26: Plotting the relation between two variables as a scatterplot with trend line.

A third geom that could be added to Figure 9.26 is geom_rug(). Figure 9.27 shows distribution information on the axes, in addition to the scatterplot with a (linear) trend line:

ggplot(pg) +
  geom_smooth(aes(x = body_mass_g, y = flipper_length_mm), method = "lm") + 
  geom_point(aes(x = body_mass_g, y = flipper_length_mm)) + 
  geom_rug(aes(x = body_mass_g, y = flipper_length_mm), linewidth = 1, alpha = .20) + 
  labs(title = "Penguin flipper length by body mass (as scatterplot and linear trend, with value distribution)")
Plotting the relation between two variables as a scatterplot with (linear) trend line and distribution information.

Figure 9.27: Plotting the relation between two variables as a scatterplot with (linear) trend line and distribution information.

As all three geoms use the same variable mappings to x and y, we can simplify the code of Figure 9.27 as follows:

ggplot(pg, aes(x = body_mass_g, y = flipper_length_mm)) +  # shared mappings (for all geoms)
  geom_smooth(method = "lm") + 
  geom_point() + 
  geom_rug(linewidth = 1, alpha = .20) + 
  labs(title = "Penguin flipper length by body mass (as scatterplot and linear trend, with value distribution)")

Similarly, we can combine the same geoms with additional grouping variable. For instance, a better version of the linear trends of penguin flipper length by body mass for each species (Figure 9.22) is provided by Figure 9.28:

tpr_01 <- ggplot(pg) +
  geom_smooth(aes(x = body_mass_g, y = flipper_length_mm, col = species), 
              method = "lm", alpha = .20) + 
  geom_point(aes(x = body_mass_g, y = flipper_length_mm, col = species, shape = species), 
             alpha = .50, size = 2) + 
  geom_rug(aes(x = body_mass_g, y = flipper_length_mm), linewidth = 1, alpha = .20) + 
  labs(title = "Penguin flipper length by body mass for each species (as linear trends)",
       x = "Body mass (in g)", y = "Flipper length (in mm)", 
       col = "Species:", shape = "Species:") +
  scale_color_manual(values = my_cols) + 
  theme_unikn()
tpr_01
Plotting scatterplot with linear trends and distribution information with an aesthetic grouping variable (species).

Figure 9.28: Plotting scatterplot with linear trends and distribution information with an aesthetic grouping variable (species).

Note that Figure 9.28 did used the aesthetic mapping col = species in geom_smooth() and ingeom_point(), but _not_ ingeom_rug(). As a consequence, the rugs (positioned by default on the $x$- and $y$-axis) provide overall distribution of values, rather than being grouped by species. Again, we can simplify the code by moving shared aesthetic mappings into the initialggplot()` function:

tpr_02 <- ggplot(pg, aes(x = body_mass_g, y = flipper_length_mm)) +
  geom_smooth(aes(col = species), method = "lm",  alpha = .20) + 
  geom_point(aes(col = species, shape = species), alpha = .50, size = 2) + 
  geom_rug(linewidth = 1, alpha = .20) + 
  labs(title = "Penguin flipper length by body mass for each species (as linear trends)",
       x = "Body mass (in g)", y = "Flipper length (in mm)", 
       col = "Species:", shape = "Species:") +
  scale_color_manual(values = my_cols) + 
  theme_unikn()
tpr_02

9.3.3 Faceting

Plots showing a lot of data or multiple geoms can become rather crowded. When geoms no longer suffice, we can easily split plots into sub-plots (so-called panels).

A truly powerful feature of ggplot2 is the ability to split plots into sub-plots by so-called faceting. We have seen above that mapping data variables to aesthetic features (like color, fill, or shape) results in grouping. Whereas this grouping occurs within a plot, faceting uses data variables to group a plot into multiple sub-plots.

Reconsidering the histogram of Figure 9.7 (saved as pg_4 above):

# Using a color-coded histogram (defined above):
pg_4

Modifying the overall histogram (of Figure 9.4) by mapping the fill color to the species variable (in Figure 9.7) revealed additional information, but also made the frequency counts harder to interpret (as some bars included counts from only one, others from two or three different species). We can disentangle both aspects (frequency of values and species) by splitting the visualization into panels by using the facet_wrap() function:

# Explicit grouping/splitting by 1 faceting variable:
pg_4 + 
  facet_wrap(~species)

The three panels allow comparing frequency counts within and between species (on the \(y\)-axis shared by all panels), as well as their relative positions (on an identical \(x\)-axis for all panels). Note that we provided the species variable as an argument to facet_wrap() in the formula notation ~species (i.e., preceded by the squiggly tilde symbol ~) and that the automatic panel headings render the legend for the fill color redundant (so that we could remove it by adding theme(legend.position = "none")).

We can extend the strategy of splitting a visualization by a variable into panels to additional variables. The facet_grid() function creates a matrix of panels

# Explicit grouping/splitting by a faceting grid:
pg_4 + 
  facet_grid(island ~ species) 
A histogram split into 3 x 3 facets.

Figure 9.29: A histogram split into 3 x 3 facets.

Note that facets split a visualization into sub-plots that use the same axes. This both de-clutters plots and allows comparisons across rows or columns.

A rather complex task:

  • Visualize the relationship between bill length and bill depth for the 3 species and islands (and sex).

Consider using automatic grouping by aesthetics or by explicitly setting group to a factor variable.

Solution: Group by island and species (and sex):

ggplot(pg, aes(x = bill_length_mm, y = bill_depth_mm, color = sex)) + 
  facet_grid(island~species) + 
  geom_point() + 
  geom_smooth(method = "lm", se = FALSE) + 
  # geom_rug() + 
  scale_color_manual(values = usecol(c(Pinky, Seeblau, Seegruen), alpha = .50)) + 
  labs(x = "Bill length (mm)", y = "Bill depth (mm)", color = "Sex:") + 
  theme_unikn()

9.3.4 Even more features

In case you have not been convinced yet, here are some additional features that make ggplot2 the greatest thing since sliced bread:

  • Alternative coordinate systems

  • Combining plots: Extend notion of a grid from faceting to several independent plots

Combining plots

Above, we have occasionally saved the output of ggplot() expressions as R objects. This allows for getting the basic plot right (i.e., selecting geoms and mapping aesthetics) before adding more bells and whistles (e.g., colors, labels, and a theme).

Another good reason for saving plots as R objects is to combine multiple plots later. Combining plots differs from faceting, as the combined plot do not need to share the same axes and coordinate system. Instead, we can combine and arrange arbitrary plots into the sub-panels of a compound figure.

In this section, we provide examples using the patchwork R package (Pedersen, 2024), but the ggpubr, cowplot and gridExtra packages provide similar functionality. For instance, if we wanted to re-capitulate the journey from our first histogram (Figure 9.3 above) to our final version of it (Figure 9.7) we could re-create and save the former as an R object pg_0 and combine it with our final histogram, which was saved as R object pg_4 (Figure 9.30):

# Re-create basic histogram (from above), but store it as pg_0:
pg_0 <- ggplot(data = pg) + 
  geom_histogram(mapping = aes(x = flipper_length_mm))

library(patchwork)  # for combining plots

# Combine 2 plots:
# pg_0 + pg_4  # beside each other
pg_0 / pg_4    # above each other
Combining two ggplot2 plots (using the patchwork package).

Figure 9.30: Combining two ggplot2 plots (using the patchwork package).

Using the gridExtra package (Auguie, 2017), we could have achieved the similar results by the grid.arrange() function:

library(gridExtra)  # for combining plots

# Combine 2 plots:
# gridExtra::grid.arrange(pg_0, pg_4, nrow = 1)
gridExtra::grid.arrange(pg_0, pg_4, nrow = 2)

However, a neat aspect of patchwork is that the height or width of sub-plots are automatically scaled to the same size.

When combining multiple plots, we usually want to arrange, annotate, or tag them, so that we can easily see and refer to their components. The patchwork package provides rich options for laying out and annotating plots. The following example also shows that it usually pays off to use a uniform color scheme and theme when combining plots (Figure 9.31):

# Create 3 plots (with common colors and theme): 
bx_1 <- ggplot(pg) +  
  geom_boxplot(aes(x = species, y = body_mass_g, fill = species)) + 
  labs(x = "Species", y = "Body mass") +
  scale_fill_manual(values = my_cols) + 
  theme_bw() + 
  theme(legend.position = "none")

bx_2 <- ggplot(pg) + 
  geom_boxplot(aes(x = species, y = flipper_length_mm, fill = species)) + 
  labs(x = "Species", y = "Flipper length") +
  scale_fill_manual(values = my_cols) + 
  theme_bw() + 
  theme(legend.position = "none")

st_1 <- ggplot(pg, aes(x = body_mass_g, y = flipper_length_mm, col = species)) + 
  geom_point() + 
  geom_smooth(method = "lm", se = FALSE) + 
  labs(x = "Body mass", y = "Flipper length") +
  scale_color_manual(values = my_cols) + 
  theme_bw() + 
  theme(legend.position = "none")


# Combine plots:
patch_plot <- (bx_1 | bx_2) / st_1  # 2 plots above 1 wide plot

# Annotate: Title(s) and caption
patch_plot <- patch_plot + 
  plot_annotation(title = "Body mass and flipper length in penguins",
                  caption = "Note: Nice, but not too surprising.")

# Tag (basic):
# patch_plot + 
#   plot_annotation(tag_levels = 'A')  # Options: '1', 'a' 'A', 'i' 'I'

# Tag (nested layout): 
patch_plot[[1]] <- patch_plot[[1]] + plot_layout(tag_level = 'new')
patch_plot + plot_annotation(tag_levels = c('A', '1'))
Combining annotated and tagged plots (using the patchwork package).

Figure 9.31: Combining annotated and tagged plots (using the patchwork package).

Note that Figure 9.31 omitted all color legends to save space. As a consequence, the scatterplot and linear trend lines of Panel B would not be interpretable when shown in isolation, but the mapping of colors to the three penguin species is explained by the boxplots shown as Panels A1 and A2.

See Chapter 9: Arranging plots of the ggplot 2 book (3e) for more patchwork examples.

Saving plots

Extensions

A powerful aspect of ggplot2 is that it can be extended by other packages, which can provide all kinds of elements, including geoms, themes, or fonts. Here are some examples:

  • ggridges provides a geom_density_ridges() that allows visualizations of variable distributions on different levels:
# install.packages('ggridges')
library(ggridges)

ggplot(pg, aes(x = flipper_length_mm, y = species, fill = species)) + 
  # facet_wrap(~species) + 
  geom_density_ridges() + 
  scale_fill_manual(values = my_cols) + 
  labs(title = "Distributions of penguin flipper length by species", 
       x = "Flipper length (mm)", y = "Species", fill = "Species:") + 
  theme_unikn()

Themes:

  • ggthemes for many additional and fancy themes:
# install.packages('ggthemes')
library(ggthemes)

pg_4 +
  theme_fivethirtyeight()

Fonts:

  • The extrafont package provides additional fonts for plotting (e.g., to mimics the visual style of the popular XKCD comic)

Practice

  1. Layering geoms: Layers of geoms

  2. Faceting: Use facets to split Figure 9.21 into three subplots showing the trends and points for each species (and remove the obsolete legend).

Since Figure 9.21 was saved as an R object tp_03 (above), we can easily add faceting by species (by adding facet_wrap()) and remove the plot legend (by a corresponding theme() function):

tp_03 + 
  facet_wrap(~species) + 
  theme(legend.position = "none")
A faceted version of Figure 9.21.

Figure 9.32: A faceted version of Figure 9.21.

  1. Coordinate systems
  1. Combining plots: Use the histograms showing the distributions of bill depth and bill length for penguins by island (from practising distributions, Task 2 above) and combine them into a single plot.

    • Print both plots side-by-side (i.e., in two columns of a single row).
    • If both plots show the same legend, remove one of them to only show one legend (on the right).

Saving plots

This concludes our glimpse into the more advanced features of ggplot2.

9.4 Conclusion

As ggplot2 currently contains over 50 different geoms, the ones discussed in this chapter provide only an introductory glimpse of the available options.

The true power of ggplot2 results from its modular and extensible structure: It provides a set of tools that can be flexibly combined to create many different visualizations.

9.4.1 Summary

The R package ggplot2 provides a comprehensive toolbox for producing data visualizations. Unlike the collection of functions in base R graphics, ggplot2 uses a conceptual framework based on the grammar of graphics (Wilkinson, 2005). This allows us to construct a graph from composable elements, instead of being limited to a predefined set of charts.

Learning ggplot2 first involves getting a grasp on its terminology (e.g., aesthetic mappings, geoms, themes, layers, and facets) and its way of combining functions to create visualizations. Figure 9.33 merely repeats Figure 9.2 (from above). Hopefully, the terms used in the figure and the interplay between the layers will now seem a bit more familiar.

The layered structure of plots in ggplot2.

Figure 9.33: The layered structure of plots in ggplot2.

A smart strategy when creating visualizations with ggplot2 for some data is to first select appropriate geoms and adjust variable mappings, before tuning aesthetics, labels, and themes.

Here’s an updated version of our initial table in Section 9.1.1 (above) that includes possible geoms of ggplot2:

Common visualization tasks and types with corresponding ggplot2 geoms.
Task: Visualize… Type of visualization In ggplot2
distributions histogram geom_histogram()
geom_density()
geom_freqpoly()
geom_violin()
geom_rug()
summaries bar chart geom_bar()
geom_col()
box plot geom_boxplot()
relations scatterplot geom_points()
line plot geom_line()
geom_abline()
geom_hline()
geom_vline()
geom_path()
geom_function()
trend line geom_smooth()

Many more geoms exist — and many geoms can be used for more than one type of visualization.

Overall, using ggplot2 implies using geoms and aesthetic mappings for solving visualization tasks. Mastering the grammar of graphics provides us with a powerful toolbox for creating informative and beautiful visualizations.

9.4.2 Resources

i2ds: Links to resources, etc.

Books and book chapters

The two main references on ggplot2 and its history are Wilkinson (2005) and Wickham (2016).

Introductory chapters on ggplot2 include:

As the original ggplot package was a pre-cursor of the so-called tidyverse dialect or movement (Wickham et al., 2019), corresponding textbooks provide good introductions to ggplot2:

Online resources

One of the best starting points for learning ggplot2 is https://ggplot2.tidyverse.org/ and its vignettes:

Note also the helpful FAQ sections in the articles of https://ggplot2.tidyverse.org

Helpful insights into the relation between geoms and stats are provided by the following article:

Further inspirations and tools for using ggplot2 include:

Cheatsheets

Here are some pointers to related Posit cheatsheets:

  • Data visualization with ggplot2
Data visualization with ggplot2 from Posit cheatsheets.

Figure 9.34: Data visualization with ggplot2 from Posit cheatsheets.

The corresponding online reference provides an overview of key ggplot2 functionality.

9.4.3 Preview

We now learned to create visualizations in base R (in Chapter 8) and the ggplot2 package. Irrespective of the tools we use, colors are an important aesthetic for making more informative and pleasing visualizations. Chapter 10 on Using colors introduces the topic of color representation and show us how to find and manipulate color palettes.

9.5 Exercises

i2ds: Exercises

Basic exercises

9.5.1 Re-creating a base R histogram

In Chapter 8, we created our first histogram for a vector of numeric values x as follows:

x <- rnorm(n = 500, mean = 100, sd = 10)
hist(x)
  1. Re-create an analog histogram in ggplot2.
  2. What are the similarities and differences to the base R version?
  3. Add some aesthetics and labels to improve your histogram.
  4. Bonus: Discuss the relation between (and the use of stats in) histograms and bar charts.

Hint: In this example, the data x consisted of a single vector. However, as ggplot() requires its data to be in tabular form, we use data.frame() to convert it into a data frame with one variable x:

# Convert vector x into df:
df <- data.frame(x)
head(df)
#>           x
#> 1 105.55735
#> 2 106.71259
#> 3  90.51432
#> 4 111.84809
#> 5  94.10383
#> 6 114.64747

Now we can fill in the minimal template and use the geom_histogram() function for creating a histogram.

9.5.2 Penguin bill distributions

Using the penguins data from palmerpenguins, create a (series of) histogram(s) that show(s) the distribution of bill depth and bill length for penguins by island. For each histogram,

  • Explain your choices of arguments and variable mappings,
  • Which aesthetic settings are provided as constants vs. variables? Why?

Hint: A solution could look as follows:

9.5.3 Basic bar chart and box plot

Using the penguins data from palmerpenguins, summarize the relation between penguin’s flipper length and species:

  1. Simple bar chart: Create a bar chart showing the average flipper length for each species of penguins.

  2. Simple box plot: Create a box plot showing the same relationship. What additional information does this type of plot show?

9.5.4 Basic scatterplot

In Chapter 8, we created a scatterplot for a vector of numeric values x and y as follows:

# Data:
x <- 11:43
y <- c(sample(5:15), sample(10:20), sample(15:25))

# Scatterplot (with base R aesthetics):
plot(x = x, y = y,  # variable mappings
     pch = 21, bg = unikn::Pinky, cex = 2, # aesthetics 
     main = "A positive correlation")
grid()
  1. Re-create an analog scatterplot in ggplot2.

  2. Create a scatterplot of fuel consumption on the highway (hwy) by engine displacement (displ) for the mpg data (in ggplot2::mpg) in base R or ggplot2.

Solution

  • ad 1. Here is an analog solution using ggplot2:

9.5.5 Line plots

In this exercise, you will create line plots for data tracking the development of five trees over time. Create some line plots using the Orange data (from base R’s datasets package):

  1. Inspect the Orange data and extract (or filter) the lines for one tree to plot a line of its circumference by age.

  2. Create an analog plot to shows the growth of all five trees (as five different lines).

  3. Adjust the line plot of 3. so that it is legible (i.e., its lines are distinguishable) in black-and-white print.

  1. Adjust your line plot (of 2. or 3.) so that an additional box plot shows the average growth of the five trees. (Hint: The group aesthetic for geom_boxplot() will be different from the group aesthetic of geom_line().)

  2. Use the same Orange data to illustrate the related geoms geom_path(), geom_step(), and geom_smooth(). What are their similarities or differences to geom_line()?

Solution

  • ad 1: Inspecting the Orange data:
# Data:
as_tibble(Orange)
#> # A tibble: 35 × 3
#>    Tree    age circumference
#>    <ord> <dbl>         <dbl>
#>  1 1       118            30
#>  2 1       484            58
#>  3 1       664            87
#>  4 1      1004           115
#>  5 1      1231           120
#>  6 1      1372           142
#>  7 1      1582           145
#>  8 2       118            33
#>  9 2       484            69
#> 10 2       664           111
#> # ℹ 25 more rows

# Note: Tree is a factor variable with a strange order of levels:
Orange$Tree
#>  [1] 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5
#> Levels: 3 < 1 < 5 < 2 < 4

# Relevel Tree factor:
Orange$Tree <- factor(Orange$Tree, levels = 1:5)
  • ad 2: Figure 9.35 shows the growth of Orange trees as a line plot:
A line plot illustrating the growth of Orange trees.

Figure 9.35: A line plot illustrating the growth of Orange trees.

9.5.6 Inspecting participant information

Use the participant information data of available as posPsy_p_info in the R package ds4psy to create some plots that describe a sample of participants.

  1. Study the data documentation of ?ds4psy::posPsy_p_info:
    How many observations and variables are there? What do the variables and their values mean? Which variables are independent (treatment) variables and which are control or dependent (outcome) variables?

  2. Create and interpret histograms to visualize the distributions of the age, educ, and income variables. Then group these distributions by intervention or sex and interpret them again.

  3. Create bar charts or box plots to inspect and interpret the average values of age, educ, and income by intervention or sex.

Make sure that all your plots provide informative axes, titles, and text labels.

The following table shows the first observations in the data:

Table 9.4: The first cases of the posPsy_p_info dataset.
id intervention sex age educ income
1 4 2 35 5 3
2 1 1 59 1 1
3 4 1 51 4 3
4 3 1 50 5 2
5 2 2 58 5 2
6 1 1 31 5 1

Note: For background and source information of the positive psychology dataset, see Appendix B.1 of the ds4psy book (Neth, 2023a).

Advanced exercises

9.5.7 Better summary charts

This exercise improves the basic summary charts from above (Section 9.5.3):

  1. Better bar chart: Transform the pg data to compute means, SE values, and corresponding confidence intervals for the flipper_length_mm variable, then use geom_bar() and geom_errorbar() to plot these means with confidence intervals.
  1. Better box plot: Provide a box plot showing the average flipper length for each species of penguins, but also information on their raw values and distributions.

Solution

Hint: The following function allows computing the standard error (SE) of a variable:

# SE formula:
std_err <- function(x, na.rm = FALSE) {

  # Handle NA values:
  if (na.rm){
    
  nr_na <- sum(is.na(x))
  
  if (nr_na > 0){
  
    x <- stats::na.omit(x)
    
    message(paste0("Removed ", nr_na, " NA values."))
  }
  
  }

  # Compute SE:   
  sqrt(stats::var(x, na.rm = na.rm)/length(x))
  
} # std_err().

# Check:
std_err(pg$body_mass_g)
std_err(pg$body_mass_g, na.rm = TRUE)
std_err(pg$flipper_length_mm, na.rm = TRUE)

# Compute summaries for flipper_length_mm of penguins per species:
tb_2 <- pg %>% 
  group_by(species) %>%
  summarise(n = n(),
            mean_flipper_length = mean(flipper_length_mm, na.rm = TRUE),
            se_flipper_length = std_err(flipper_length_mm, na.rm = TRUE), 
            mn_conf_min = mean_flipper_length - 1.96 * se_flipper_length,
            mn_conf_max = mean_flipper_length + 1.96 * se_flipper_length
            ) 
tb_2

9.5.8 The rule of 72

In finance, the rule of 72 is a heuristic strategy for estimating the doubling time of an investment. Dividing the number (72) by the interest percentage per period (usually years) yields the approximate number of periods required for doubling the initial investment. (See Wikipedia for details: en | de.)

  • Create a line graph that compares the true doubling time with the heuristic estimates for a range of (positive) interest rates.

Hints:

  • Consider wrapping your code into a function.
  • Consider creating an interactive application of your function (e.g., using Shiny, see Chapter 25).

9.5.9 Advanced ggplot expressions

The following ggplot() expressions are copied from the documentation of the corresponding geoms. Run the code, inspect the result, and then try to explain how they work:

  1. A facet of histograms:
ggplot(economics_long, aes(value)) +
  facet_wrap(~variable, scales = 'free_x') +
  geom_histogram(binwidth = function(x) 2 * IQR(x) / (length(x)^(1/3)))

9.5.10 Horse trading

A notorious problem studied in psychology is the following (e.g., Maier & Burke, 1967, p. 305):

A man bought a horse for $60 and sold it for $70.
Then he bought it back again for $80 and sold it for $90.
How much money did he make in the horse business?

Create a visualization that illustrates all four transactions and the problem’s solution.

  • How can we see the correct solution?
  • Why do many people provide a different solution?

Hint: The problem’s key data could be represented as follows:

Table 9.5: The data of the horse trading problem.
nr type object price
1 buy horse 60
2 sell horse 70
3 buy horse 80
4 sell horse 90

9.5.11 Bonus: Anscombe’s quartet again

An exercise of the previous chapter (Section 8.5.10) re-created the Anscombe plots of Figure 7.1 by using the data from datasets::anscombe and base R functions. However, the original Figure 7.1 (in Section 7.2.1) was actually created in ggplot2. Hence, try to re-create the following figure from the data in datasets::anscombe:

Scatterplots of the four subsets. (The \(+\)-symbol marks the mean of each set; blue lines indicate the best fitting linear regression line.)

Figure 9.36: Scatterplots of the four subsets. (The \(+\)-symbol marks the mean of each set; blue lines indicate the best fitting linear regression line.)

Hint: We first need to transform the data in datasets::anscombe into a longer format (which uses two variables for x- and y-values and a separate factor variable set that indicates the identity or number of the set).

More exercises

For even more exercises on using ggplot2,


  1. For more detailed explanations of the connection between geoms and stats, see the ggplot2 documentation or the online article Demystifying stat layers in ggplot2 (by June Choe, 2020-09-26).↩︎

  2. Note the relation between the mathematical and the computational notion of a function: The scatterplot visualization shows a relation by mapping values on some dimension \(x\) to values on some dimension \(y\). If we view the \(x\)-values as inputs and the \(y\)-values as outputs, the underlying function is the relation that transforms the former into the latter.↩︎