r4ds/data-visualize.qmd

933 lines
41 KiB
Plaintext
Raw Normal View History

2022-12-05 15:46:05 +08:00
# Data visualization {#sec-data-visualization}
```{r}
#| results: "asis"
#| echo: false
source("_common.R")
2022-12-05 02:05:38 +08:00
status("complete")
```
2015-12-12 02:34:20 +08:00
2016-07-22 05:54:19 +08:00
## Introduction
> "The simple graph has brought more information to the data analyst's mind than any other device." --- John Tukey
R has several systems for making graphs, but ggplot2 is one of the most elegant and most versatile.
ggplot2 implements the **grammar of graphics**, a coherent system for describing and building graphs.
With ggplot2, you can do more and faster by learning one system and applying it in many places.
This chapter will teach you how to visualize your data using **ggplot2**.
2022-12-05 02:05:38 +08:00
We will start by creating a simple scatterplot and use that to introduce aesthetic mappings and geometric objects -- the fundamental building blocks of ggplot2.
We will then walk you through visualizing distributions of single variables as well as visualizing relationships between two or more variables.
We'll finish off with saving your plots and troubleshooting tips.
2016-07-22 05:54:19 +08:00
### Prerequisites
This chapter focuses on ggplot2, one of the core packages in the tidyverse.
To access the datasets, help pages, and functions used in this chapter, load the tidyverse by running:
2022-03-05 08:47:48 +08:00
```{r}
#| label: setup
2016-10-04 01:30:24 +08:00
library(tidyverse)
```
That one line of code loads the core tidyverse; the packages that you will use in almost every data analysis.
It also tells you which functions from the tidyverse conflict with functions in base R (or from other packages you might have loaded)[^data-visualize-1].
[^data-visualize-1]: You can eliminate that message and force conflict resolution to happen on demand by using the conflicted package, which becomes more important as you load more packages.
You can learn more about conflicted at <https://conflicted.r-lib.org>.
2016-10-04 01:30:24 +08:00
If you run this code and get the error message `there is no package called 'tidyverse'`, you'll need to first install it, then run `library()` once again.
2016-07-12 06:22:37 +08:00
```{r}
#| eval: false
2022-03-05 08:47:48 +08:00
2016-10-04 01:30:24 +08:00
install.packages("tidyverse")
library(tidyverse)
```
You only need to install a package once, but you need to load it every time you start a new session.
2016-07-12 06:22:37 +08:00
In addition to tidyverse, we will also use the **palmerpenguins** package, which includes the `penguins` dataset containing body measurements for penguins on three islands in the Palmer Archipelago, and the ggthemes package, which offers a colorblind safe color palette.
2022-12-05 02:05:38 +08:00
```{r}
library(palmerpenguins)
library(ggthemes)
2022-12-05 02:05:38 +08:00
```
2016-10-05 00:08:12 +08:00
## First steps
Do penguins with longer flippers weigh more or less than penguins with shorter flippers?
You probably already have an answer, but try to make your answer precise.
2022-12-05 02:05:38 +08:00
What does the relationship between flipper length and body mass look like?
Is it positive?
Negative?
Linear?
Nonlinear?
2022-12-05 02:05:38 +08:00
Does the relationship vary by the species of the penguin?
How about by the island where the penguin lives?
Let's create visualizations that we can use to answer these questions.
2022-12-05 02:05:38 +08:00
### The `penguins` data frame
2016-10-05 00:08:12 +08:00
You can test your answers to those questions with the `penguins` **data frame** found in palmerpenguins (a.k.a. `palmerpenguins::penguins`).
A data frame is a rectangular collection of variables (in the columns) and observations (in the rows).
`penguins` contains `r nrow(penguins)` observations collected and made available by Dr. Kristen Gorman and the Palmer Station, Antarctica LTER[^data-visualize-2].
2016-07-12 06:22:37 +08:00
[^data-visualize-2]: Horst AM, Hill AP, Gorman KB (2020).
2022-12-05 02:05:38 +08:00
palmerpenguins: Palmer Archipelago (Antarctica) penguin data.
R package version 0.1.0.
<https://allisonhorst.github.io/palmerpenguins/>.
doi: 10.5281/zenodo.3960218.
To make the discussion easier, let's define some terms:
- A **variable** is a quantity, quality, or property that you can measure.
- A **value** is the state of a variable when you measure it.
The value of a variable may change from measurement to measurement.
- An **observation** is a set of measurements made under similar conditions (you usually make all of the measurements in an observation at the same time and on the same object).
An observation will contain several values, each associated with a different variable.
We'll sometimes refer to an observation as a data point.
- **Tabular data** is a set of values, each associated with a variable and an observation.
Tabular data is *tidy* if each value is placed in its own "cell", each variable in its own column, and each observation in its own row.
In this context, a variable refers to an attribute of all the penguins, and an observation refers to all the attributes of a single penguin.
Type the name of the data frame in the console and R will print a preview of its contents.
Note that it says `tibble` on top of this preview.
In the tidyverse, we use special data frames called **tibbles** that you will learn more about soon.
```{r}
2022-12-05 02:05:38 +08:00
penguins
```
2022-12-05 02:05:38 +08:00
This data frame contains `r ncol(penguins)` columns.
For an alternative view, where you can see all variables and the first few observations of each variable, use `glimpse()`.
Or, if you're in RStudio, click on the name of the data frame in the Environment pane or run `View(penguins)` to open an interactive data viewer.
```{r}
2022-12-05 02:05:38 +08:00
glimpse(penguins)
```
2022-12-05 02:05:38 +08:00
Among the variables in `penguins` are:
2016-10-04 05:08:44 +08:00
2022-12-05 02:05:38 +08:00
1. `species`: a penguin's species (Adelie, Chinstrap, or Gentoo).
2016-07-22 05:54:19 +08:00
2022-12-05 02:05:38 +08:00
2. `flipper_length_mm`: length of a penguin's flipper, in millimeters.
2022-12-05 02:05:38 +08:00
3. `body_mass_g`: body mass of a penguin, in grams.
2016-07-22 05:54:19 +08:00
2022-12-05 02:05:38 +08:00
To learn more about `penguins`, open its help page by running `?penguins`.
2022-12-05 02:05:38 +08:00
### Ultimate goal {#sec-ultimate-goal}
2022-12-05 02:05:38 +08:00
Our ultimate goal in this chapter is to recreate the following visualization displaying the relationship between flipper lengths and body masses of these penguins, taking into consideration the species of the penguin.
```{r}
#| echo: false
2022-12-05 02:05:38 +08:00
#| warning: false
#| fig-alt: >
#| A scatterplot of body mass vs. flipper length of penguins, with a
#| best fit line of the relationship between these two variables
#| overlaid. The plot displays a positive, fairly linear, and relatively
2022-12-05 02:05:38 +08:00
#| strong relationship between these two variables. Species (Adelie,
#| Chinstrap, and Gentoo) are represented with different colors and
#| shapes. The relationship between body mass and flipper length is
#| roughly the same for these three species, and Gentoo penguins are
#| larger than penguins from the other two species.
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = species)) +
geom_smooth(method = "lm") +
2022-12-05 02:05:38 +08:00
labs(
title = "Body mass and flipper length",
subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins",
x = "Flipper length (mm)",
2022-12-07 03:10:24 +08:00
y = "Body mass (g)",
2022-12-05 02:05:38 +08:00
color = "Species",
shape = "Species"
) +
scale_color_colorblind()
```
2022-12-05 02:05:38 +08:00
### Creating a ggplot
Let's recreate this plot step-by-step.
With ggplot2, you begin a plot with the function `ggplot()`, defining a plot object that you then add **layers** to.
The first argument of `ggplot()` is the dataset to use in the graph and so `ggplot(data = penguins)` creates an empty graph that is primed to display the `penguins` data, but since we haven't told it how to visualize it yet, for now it's empty.
2022-12-05 02:05:38 +08:00
This is not a very exciting plot, but you can think of it like an empty canvas you'll paint the remaining layers of your plot onto.
```{r}
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A blank, gray plot area.
ggplot(data = penguins)
2015-12-12 03:28:10 +08:00
```
Next, we need to tell `ggplot()` how the information from our data will be visually represented.
The `mapping` argument of the `ggplot()` function defines how variables in your dataset are mapped to visual properties (**aesthetics**) of your plot.
The `mapping` argument is always defined in the `aes()` function, and the `x` and `y` arguments of `aes()` specify which variables to map to the x and y axes.
2022-12-05 02:05:38 +08:00
For now, we will only map flipper length to the `x` aesthetic and body mass to the `y` aesthetic.
ggplot2 looks for the mapped variables in the `data` argument, in this case, `penguins`.
The following plot shows the result of adding these mappings.
```{r}
#| fig-alt: >
#| The plot shows flipper length on the x-axis, with values that range from
#| 170 to 230, and body mass on the y-axis, with values that range from 3000
#| to 6000.
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
)
2022-12-05 02:05:38 +08:00
```
2016-07-12 06:22:37 +08:00
2022-12-05 02:05:38 +08:00
Our empty canvas now has more structure -- it's clear where flipper lengths will be displayed (on the x-axis) and where body masses will be displayed (on the y-axis).
But the penguins themselves are not yet on the plot.
This is because we have not yet articulated, in our code, how to represent the observations from our data frame on our plot.
2022-12-05 02:05:38 +08:00
To do so, we need to define a **geom**: the geometrical object that a plot uses to represent data.
These geometric objects are made available in ggplot2 with functions that start with `geom_`.
People often describe plots by the type of geom that the plot uses.
For example, bar charts use bar geoms (`geom_bar()`), line charts use line geoms (`geom_line()`), boxplots use boxplot geoms (`geom_boxplot()`), scatterplots use point geoms (`geom_point()`), and so on.
2022-12-05 02:05:38 +08:00
The function `geom_point()` adds a layer of points to your plot, which creates a scatterplot.
ggplot2 comes with many geom functions that each adds a different type of layer to a plot.
You'll learn a whole bunch of geoms throughout the book, particularly in @sec-layers.
```{r}
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A scatterplot of body mass vs. flipper length of penguins. The plot
#| displays a positive, linear, and relatively strong relationship between
2022-12-05 02:05:38 +08:00
#| these two variables.
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
2022-12-05 02:05:38 +08:00
geom_point()
```
2022-12-05 02:05:38 +08:00
Now we have something that looks like what we might think of as a "scatter plot".
It doesn't yet match our "ultimate goal" plot, but using this plot we can start answering the question that motivated our exploration: "What does the relationship between flipper length and body mass look like?" The relationship appears to be positive (as flipper length increases, so does body mass), fairly linear (the points are clustered around a line instead of a curve), and moderately strong (there isn't too much scatter around such a line).
2022-12-05 02:05:38 +08:00
Penguins with longer flippers are generally larger in terms of their body mass.
Before we add more layers to this plot, let's pause for a moment and review the warning message we got:
> Removed 2 rows containing missing values (`geom_point()`).
We're seeing this message because there are two penguins in our dataset with missing body mass and/or flipper length values and ggplot2 has no way of representing them on the plot without both of these values.
2022-12-05 02:05:38 +08:00
Like R, ggplot2 subscribes to the philosophy that missing values should never silently go missing.
This type of warning is probably one of the most common types of warnings you will see when working with real data -- missing values are a very common issue and you'll learn more about them throughout the book, particularly in @sec-missing-values.
For the remaining plots in this chapter we will suppress this warning so it's not printed alongside every single plot we make.
2015-11-19 06:01:07 +08:00
### Adding aesthetics and layers {#sec-adding-aesthetics-layers}
Scatterplots are useful for displaying the relationship between two numerical variables, but it's always a good idea to be skeptical of any apparent relationship between two variables and ask if there may be other variables that explain or change the nature of this apparent relationship.
For example, does the relationship between flipper length and body mass differ by species?
Let's incorporate species into our plot and see if this reveals any additional insights into the apparent relationship between these variables.
2022-12-05 02:05:38 +08:00
We will do this by representing species with different colored points.
To achieve this, will we need to modify the aesthetic or the geom?
2022-12-05 02:05:38 +08:00
If you guessed "in the aesthetic mapping, inside of `aes()`", you're already getting the hang of creating data visualizations with ggplot2!
And if not, don't worry.
Throughout the book you will make many more ggplots and have many more opportunities to check your intuition as you make them.
```{r}
2022-12-05 02:05:38 +08:00
#| warning: false
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A scatterplot of body mass vs. flipper length of penguins. The plot
#| displays a positive, fairly linear, and relatively strong relationship
2022-12-05 02:05:38 +08:00
#| between these two variables. Species (Adelie, Chinstrap, and Gentoo)
#| are represented with different colors.
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)
) +
2022-12-05 02:05:38 +08:00
geom_point()
```
When a categorical variable is mapped to an aesthetic, ggplot2 will automatically assign a unique value of the aesthetic (here a unique color) to each unique level of the variable (each of the three species), a process known as **scaling**.
2022-12-05 02:05:38 +08:00
ggplot2 will also add a legend that explains which values correspond to which levels.
2022-12-05 02:05:38 +08:00
Now let's add one more layer: a smooth curve displaying the relationship between body mass and flipper length.
Before you proceed, refer back to the code above, and think about how we can add this to our existing plot.
Since this is a new geometric object representing our data, we will add a new geom as a layer on top of our point geom: `geom_smooth()`.
And we will specify that we want to draw the line of best fit based on a `l`inear `m`odel with `method = "lm"`.
```{r}
#| warning: false
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A scatterplot of body mass vs. flipper length of penguins. Overlaid
#| on the scatterplot are three smooth curves displaying the
#| relationship between these variables for each species (Adelie,
#| Chinstrap, and Gentoo). Different penguin species are plotted in
#| different colors for the points and the smooth curves.
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)
) +
2022-12-05 02:05:38 +08:00
geom_point() +
geom_smooth(method = "lm")
2016-07-12 06:22:37 +08:00
```
We have successfully added lines, but this plot doesn't look like the plot from @sec-ultimate-goal, which only has one line for the entire dataset as opposed to separate lines for each of the penguin species.
When aesthetic mappings are defined in `ggplot()`, at the *global* level, they're passed down to each of the subsequent geom layers of the plot.
However, each geom function in ggplot2 can also take a `mapping` argument, which allows for aesthetic mappings at the *local* level that are added to those inherited from the global level.
Since we want points to be colored based on species but don't want the lines to be separated out for them, we should specify `color = species` for `geom_point()` only.
2022-12-05 02:05:38 +08:00
```{r}
#| warning: false
#| fig-alt: >
#| A scatterplot of body mass vs. flipper length of penguins. Overlaid
#| on the scatterplot is a single line of best fit displaying the
2022-12-05 02:05:38 +08:00
#| relationship between these variables for each species (Adelie,
#| Chinstrap, and Gentoo). Different penguin species are plotted in
#| different colors for the points only.
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
2022-12-05 02:05:38 +08:00
geom_point(mapping = aes(color = species)) +
geom_smooth(method = "lm")
2016-07-22 05:54:19 +08:00
```
2022-12-05 02:05:38 +08:00
Voila!
We have something that looks very much like our ultimate goal, though it's not yet perfect.
We still need to use different shapes for each species of penguins and improve labels.
2016-07-22 05:54:19 +08:00
2022-12-05 02:05:38 +08:00
It's generally not a good idea to represent information using only colors on a plot, as people perceive colors differently due to color blindness or other color vision differences.
Therefore, in addition to color, we can also map `species` to the `shape` aesthetic.
2022-09-08 06:19:24 +08:00
```{r}
2022-12-05 02:05:38 +08:00
#| warning: false
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A scatterplot of body mass vs. flipper length of penguins. Overlaid
#| on the scatterplot is a single line of best fit displaying the
2022-12-05 02:05:38 +08:00
#| relationship between these variables for each species (Adelie,
#| Chinstrap, and Gentoo). Different penguin species are plotted in
#| different colors and shapes for the points only.
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
2022-12-05 02:05:38 +08:00
geom_point(mapping = aes(color = species, shape = species)) +
geom_smooth(method = "lm")
```
2022-12-05 02:05:38 +08:00
Note that the legend is automatically updated to reflect the different shapes of the points as well.
And finally, we can improve the labels of our plot using the `labs()` function in a new layer.
Some of the arguments to `labs()` might be self explanatory: `title` adds a title and `subtitle` adds a subtitle to the plot.
Other arguments match the aesthetic mappings, `x` is the x-axis label, `y` is the y-axis label, and `color` and `shape` define the label for the legend.
In addition, we can improve the color palette to be colorblind safe with the `scale_color_colorblind()` function from the ggthemes package.
```{r}
2022-12-05 02:05:38 +08:00
#| warning: false
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A scatterplot of body mass vs. flipper length of penguins, with a
#| line of best fit displaying the relationship between these two variables
#| overlaid. The plot displays a positive, fairly linear, and relatively
2022-12-05 02:05:38 +08:00
#| strong relationship between these two variables. Species (Adelie,
#| Chinstrap, and Gentoo) are represented with different colors and
#| shapes. The relationship between body mass and flipper length is
#| roughly the same for these three species, and Gentoo penguins are
#| larger than penguins from the other two species.
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
2022-12-05 02:05:38 +08:00
geom_point(aes(color = species, shape = species)) +
geom_smooth(method = "lm") +
2022-12-05 02:05:38 +08:00
labs(
title = "Body mass and flipper length",
subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins",
x = "Flipper length (mm)", y = "Body mass (g)",
color = "Species", shape = "Species"
) +
scale_color_colorblind()
```
2022-12-05 02:05:38 +08:00
We finally have a plot that perfectly matches our "ultimate goal"!
2022-12-05 02:05:38 +08:00
### Exercises
2016-07-22 05:54:19 +08:00
2022-12-05 02:05:38 +08:00
1. How many rows are in `penguins`?
How many columns?
2022-12-05 02:05:38 +08:00
2. What does the `bill_depth_mm` variable in the `penguins` data frame describe?
Read the help for `?penguins` to find out.
3. Make a scatterplot of `bill_depth_mm` vs. `bill_length_mm`.
2023-03-02 13:45:25 +08:00
That is, make a scatterplot with `bill_depth_mm` on the y-axis and `bill_length_mm` on the x-axis.
2022-12-05 02:05:38 +08:00
Describe the relationship between these two variables.
2023-01-27 23:51:22 +08:00
4. What happens if you make a scatterplot of `species` vs. `bill_depth_mm`?
What might be a better choice of geom?
2022-12-05 02:05:38 +08:00
5. Why does the following give an error and how would you fix it?
```{r}
#| eval: false
2022-12-05 02:05:38 +08:00
ggplot(data = penguins) +
geom_point()
```
2022-12-05 02:05:38 +08:00
6. What does the `na.rm` argument do in `geom_point()`?
What is the default value of the argument?
Create a scatterplot where you successfully use this argument set to `TRUE`.
2022-12-05 02:05:38 +08:00
7. Add the following caption to the plot you made in the previous exercise: "Data come from the palmerpenguins package." Hint: Take a look at the documentation for `labs()`.
2022-12-05 02:05:38 +08:00
8. Recreate the following visualization.
What aesthetic should `bill_depth_mm` be mapped to?
And should it be mapped at the global level or at the geom level?
```{r}
2022-12-05 02:05:38 +08:00
#| echo: false
#| warning: false
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A scatterplot of body mass vs. flipper length of penguins, colored
#| by bill depth. A smooth curve of the relationship between body mass
#| and flipper length is overlaid. The relationship is positive,
#| fairly linear, and moderately strong.
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
2022-12-05 02:05:38 +08:00
geom_point(aes(color = bill_depth_mm)) +
geom_smooth()
```
9. Run this code in your head and predict what the output will look like.
Then, run the code in R and check your predictions.
2022-12-05 02:05:38 +08:00
```{r}
#| eval: false
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g, color = island)
) +
geom_point() +
2022-12-05 02:05:38 +08:00
geom_smooth(se = FALSE)
```
2022-12-05 02:05:38 +08:00
10. Will these two graphs look different?
Why/why not?
```{r}
2022-12-05 02:05:38 +08:00
#| eval: false
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point() +
2022-12-05 02:05:38 +08:00
geom_smooth()
ggplot() +
geom_point(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_smooth(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
)
```
2023-03-02 13:29:53 +08:00
## ggplot2 calls {#sec-ggplot2-calls}
2022-12-05 02:05:38 +08:00
As we move on from these introductory sections, we'll transition to a more concise expression of ggplot2 code.
So far we've been very explicit, which is helpful when you are learning:
```{r}
2022-12-05 02:05:38 +08:00
#| eval: false
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
2022-12-05 02:05:38 +08:00
geom_point()
```
2022-12-05 02:05:38 +08:00
Typically, the first one or two arguments to a function are so important that you should know them by heart.
The first two arguments to `ggplot()` are `data` and `mapping`, in the remainder of the book, we won't supply those names.
That saves typing, and, by reducing the amount of extra text, makes it easier to see what's different between plots.
2022-12-05 02:05:38 +08:00
That's a really important programming concern that we'll come back to in @sec-functions.
2022-12-05 02:05:38 +08:00
Rewriting the previous plot more concisely yields:
```{r}
#| eval: false
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point()
```
In the future, you'll also learn about the pipe, `|>`, which will allow you to create that plot with:
```{r}
2022-12-05 02:05:38 +08:00
#| eval: false
2022-12-05 02:05:38 +08:00
penguins |>
ggplot(aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point()
```
2022-12-05 02:05:38 +08:00
## Visualizing distributions
2022-12-05 02:05:38 +08:00
How you visualize the distribution of a variable depends on the type of variable: categorical or numerical.
2022-12-05 02:05:38 +08:00
### A categorical variable
2022-12-05 02:05:38 +08:00
A variable is **categorical** if it can only take one of a small set of values.
To examine the distribution of a categorical variable, you can use a bar chart.
The height of the bars displays how many observations occurred with each `x` value.
2016-05-19 23:31:02 +08:00
```{r}
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A bar chart of frequencies of species of penguins: Adelie
#| (approximately 150), Chinstrap (approximately 90), Gentoo
#| (approximately 125).
ggplot(penguins, aes(x = species)) +
geom_bar()
2016-05-19 23:31:02 +08:00
```
2022-12-05 02:05:38 +08:00
In bar plots of categorical variables with non-ordered levels, like the penguin `species` above, it's often preferable to reorder the bars based on their frequencies.
Doing so requires transforming the variable to a factor (how R handles categorical data) and then reordering the levels of that factor.
```{r}
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A bar chart of frequencies of species of penguins, where the bars are
#| ordered in decreasing order of their heights (frequencies): Adelie
#| (approximately 150), Gentoo (approximately 125), Chinstrap
#| (approximately 90).
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = fct_infreq(species))) +
geom_bar()
```
2022-12-05 02:05:38 +08:00
You will learn more about factors and functions for dealing with factors (like `fct_infreq()` shown above) in @sec-factors.
### A numerical variable
A variable is **numerical** (or quantitative) if it can take on a wide range of numerical values, and it is sensible to add, subtract, or take averages with those values.
Numerical variables can be continuous or discrete.
One commonly used visualization for distributions of continuous variables is a histogram.
```{r}
2022-12-05 02:05:38 +08:00
#| warning: false
#| fig-alt: >
#| A histogram of body masses of penguins. The distribution is unimodal
#| and right skewed, ranging between approximately 2500 to 6500 grams.
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = body_mass_g)) +
geom_histogram(binwidth = 200)
```
2022-12-05 02:05:38 +08:00
A histogram divides the x-axis into equally spaced bins and then uses the height of a bar to display the number of observations that fall in each bin.
In the graph above, the tallest bar shows that 39 observations have a `body_mass_g` value between 3,500 and 3,700 grams, which are the left and right edges of the bar.
2022-12-05 02:05:38 +08:00
You can set the width of the intervals in a histogram with the binwidth argument, which is measured in the units of the `x` variable.
You should always explore a variety of binwidths when working with histograms, as different binwidths can reveal different patterns.
In the plots below a binwidth of 20 is too narrow, resulting in too many bars, making it difficult to determine the shape of the distribution.
Similarly, a binwidth of 2,000 is too high, resulting in all data being binned into only three bars, and also making it difficult to determine the shape of the distribution.
A binwidth of 200 provides a sensible balance.
```{r}
2022-12-05 02:05:38 +08:00
#| warning: false
#| layout-ncol: 2
#| fig-width: 3
#| fig-alt: >
#| Two histograms of body masses of penguins, one with binwidth of 20
#| (left) and one with binwidth of 2000 (right). The histogram with binwidth
#| of 20 shows lots of ups and downs in the heights of the bins, creating a
#| jagged outline. The histogram with binwidth of 2000 shows only three bins.
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = body_mass_g)) +
geom_histogram(binwidth = 20)
ggplot(penguins, aes(x = body_mass_g)) +
geom_histogram(binwidth = 2000)
```
An alternative visualization for distributions of numerical variables is a density plot.
A density plot is a smoothed-out version of a histogram and a practical alternative, particularly for continuous data that comes from an underlying smooth distribution.
We won't go into how `geom_density()` estimates the density (you can read more about that in the function documentation), but let's explain how the density curve is drawn with an analogy.
Imagine a histogram made out of wooden blocks.
Then, imagine that you drop a cooked spaghetti string over it.
The shape the spaghetti will take draped over blocks can be thought of as the shape of the density curve.
It shows fewer details than a histogram but can make it easier to quickly glean the shape of the distribution, particularly with respect to modes and skewness.
```{r}
#| fig-alt: >
#| A density plot of body masses of penguins. The distribution is unimodal
#| and right skewed, ranging between approximately 2500 to 6500 grams.
ggplot(penguins, aes(x = body_mass_g)) +
geom_density()
```
### Exercises
2022-12-05 02:05:38 +08:00
1. Make a bar plot of `species` of `penguins`, where you assign `species` to the `y` aesthetic.
How is this plot different?
2022-12-05 02:05:38 +08:00
2. How are the following two plots different?
Which aesthetic, `color` or `fill`, is more useful for changing the color of bars?
```{r}
#| eval: false
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = species)) +
geom_bar(color = "red")
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = species)) +
geom_bar(fill = "red")
2022-09-08 06:23:18 +08:00
```
2022-12-05 02:05:38 +08:00
3. What does the `bins` argument in `geom_histogram()` do?
2016-07-22 05:54:19 +08:00
4. Make a histogram of the `carat` variable in the `diamonds` dataset that is available when you load the tidyverse package.
2022-12-05 02:05:38 +08:00
Experiment with different binwidths.
What binwidth reveals the most interesting patterns?
2022-12-05 02:05:38 +08:00
## Visualizing relationships
2022-12-05 02:05:38 +08:00
To visualize a relationship we need to have at least two variables mapped to aesthetics of a plot.
In the following sections you will learn about commonly used plots for visualizing relationships between two or more variables and the geoms used for creating them.
2022-12-05 02:05:38 +08:00
### A numerical and a categorical variable
2022-12-05 02:05:38 +08:00
To visualize the relationship between a numerical and a categorical variable we can use side-by-side box plots.
A **boxplot** is a type of visual shorthand for measures of position (percentiles) that describe a distribution.
It is also useful for identifying potential outliers.
As shown in @fig-eda-boxplot, each boxplot consists of:
- A box that indicates the range of the middle half of the data, a distance known as the interquartile range (IQR), stretching from the 25th percentile of the distribution to the 75th percentile.
2022-12-05 02:05:38 +08:00
In the middle of the box is a line that displays the median, i.e. 50th percentile, of the distribution.
These three lines give you a sense of the spread of the distribution and whether or not the distribution is symmetric about the median or skewed to one side.
2022-12-05 02:05:38 +08:00
- Visual points that display observations that fall more than 1.5 times the IQR from either edge of the box.
These outlying points are unusual so are plotted individually.
2022-12-05 02:05:38 +08:00
- A line (or whisker) that extends from each end of the box and goes to the farthest non-outlier point in the distribution.
```{r}
#| label: fig-eda-boxplot
2022-12-05 02:05:38 +08:00
#| echo: false
#| fig-cap: >
#| Diagram depicting how a boxplot is created.
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A diagram depicting how a boxplot is created following the steps outlined
#| above.
2022-12-05 02:05:38 +08:00
knitr::include_graphics("images/EDA-boxplot.png")
2015-11-13 06:44:03 +08:00
```
Let's take a look at the distribution of body mass by species using `geom_boxplot()`:
```{r}
2022-12-05 02:05:38 +08:00
#| warning: false
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| Side-by-side box plots of distributions of body masses of Adelie,
#| Chinstrap, and Gentoo penguins. The distribution of Adelie and
#| Chinstrap penguins' body masses appear to be symmetric with
#| medians around 3750 grams. The median body mass of Gentoo penguins
#| is much higher, around 5000 grams, and the distribution of the
#| body masses of these penguins appears to be somewhat right skewed.
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = species, y = body_mass_g)) +
geom_boxplot()
```
Alternatively, we can make density plots with `geom_density()`.
```{r}
2022-12-05 02:05:38 +08:00
#| warning: false
#| fig-alt: >
#| A density plot of body masses of penguins by species of penguins. Each
#| species (Adelie, Chinstrap, and Gentoo) is represented with different
#| colored outlines for the density curves.
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = body_mass_g, color = species)) +
geom_density(linewidth = 0.75)
```
2015-11-13 06:44:03 +08:00
2022-12-05 02:05:38 +08:00
We've also customized the thickness of the lines using the `linewidth` argument in order to make them stand out a bit more against the background.
Additionally, we can map `species` to both `color` and `fill` aesthetics and use the `alpha` aesthetic to add transparency to the filled density curves.
2022-12-05 02:05:38 +08:00
This aesthetic takes values between 0 (completely transparent) and 1 (completely opaque).
In the following plot it's *set* to 0.5.
2015-11-13 06:44:03 +08:00
2022-12-05 02:05:38 +08:00
```{r}
#| warning: false
#| fig-alt: >
#| A density plot of body masses of penguins by species of penguins. Each
#| species (Adelie, Chinstrap, and Gentoo) is represented in different
#| colored outlines for the density curves. The density curves are also
#| filled with the same colors, with some transparency added.
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = body_mass_g, color = species, fill = species)) +
geom_density(alpha = 0.5)
```
2022-12-05 02:05:38 +08:00
Note the terminology we have used here:
2022-12-05 02:05:38 +08:00
- We *map* variables to aesthetics if we want the visual attribute represented by that aesthetic to vary based on the values of that variable.
- Otherwise, we *set* the value of an aesthetic.
2022-12-05 02:05:38 +08:00
### Two categorical variables
We can use stacked bar plots to visualize the relationship between two categorical variables.
For example, the following two stacked bar plots both display the relationship between `island` and `species`, or specifically, visualizing the distribution of `species` within each island.
The first plot shows the frequencies of each species of penguins on each island.
The plot of frequencies show that there are equal numbers of Adelies on each island.
But we don't have a good sense of the percentage balance within each island.
```{r}
#| fig-alt: >
#| Bar plots of penguin species by island (Biscoe, Dream, and Torgersen)
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = island, fill = species)) +
geom_bar()
```
The second plot is a relative frequency plot, created by setting `position = "fill"` in the geom is more useful for comparing species distributions across islands since it's not affected by the unequal numbers of penguins across the islands.
Using this plot we can see that Gentoo penguins all live on Biscoe island and make up roughly 75% of the penguins on that island, Chinstrap all live on Dream island and make up roughly 50% of the penguins on that island, and Adelie live on all three islands and make up all of the penguins on Torgersen.
```{r}
#| fig-alt: >
#| Bar plots of penguin species by island (Biscoe, Dream, and Torgersen)
#| the bars are scaled to the same height, making it a relative frequencies
#| plot
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = island, fill = species)) +
geom_bar(position = "fill")
```
In creating these bar charts, we map the variable that will be separated into bars to the `x` aesthetic, and the variable that will change the colors inside the bars to the `fill` aesthetic.
2022-12-05 02:05:38 +08:00
### Two numerical variables
2015-11-17 10:19:27 +08:00
2022-12-05 02:05:38 +08:00
So far you've learned about scatterplots (created with `geom_point()`) and smooth curves (created with `geom_smooth()`) for visualizing the relationship between two numerical variables.
A scatterplot is probably the most commonly used plot for visualizing the relationship between two numerical variables.
2022-12-05 02:05:38 +08:00
```{r}
#| warning: false
#| fig-alt: >
#| A scatterplot of body mass vs. flipper length of penguins. The plot
#| displays a positive, linear, relatively strong relationship between
#| these two variables.
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point()
```
2022-12-05 02:05:38 +08:00
### Three or more variables
As we saw in @sec-adding-aesthetics-layers, we can incorporate more variables into a plot by mapping them to additional aesthetics.
2022-12-05 02:05:38 +08:00
For example, in the following scatterplot the colors of points represent species and the shapes of points represent islands.
```{r}
2022-12-05 02:05:38 +08:00
#| warning: false
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A scatterplot of body mass vs. flipper length of penguins. The plot
#| displays a positive, linear, relatively strong relationship between
#| these two variables. The points are colored based on the species of the
#| penguins and the shapes of the points represent islands (round points are
#| Biscoe island, triangles are Dream island, and squared are Torgersen
#| island). The plot is very busy and it's difficult to distinguish the shapes
#| of the points.
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = island))
```
2022-12-05 02:05:38 +08:00
However adding too many aesthetic mappings to a plot makes it cluttered and difficult to make sense of.
Another way, which is particularly useful for categorical variables, is to split your plot into **facets**, subplots that each display one subset of the data.
2022-12-05 02:05:38 +08:00
To facet your plot by a single variable, use `facet_wrap()`.
The first argument of `facet_wrap()` is a formula[^data-visualize-3], which you create with `~` followed by a variable name.
2022-12-05 02:05:38 +08:00
The variable that you pass to `facet_wrap()` should be categorical.
[^data-visualize-3]: Here "formula" is the name of the thing created by `~`, not a synonym for "equation".
```{r}
2022-12-05 02:05:38 +08:00
#| warning: false
#| fig-width: 8
#| fig-asp: 0.33
#| fig-alt: >
2022-12-05 02:05:38 +08:00
#| A scatterplot of body mass vs. flipper length of penguins. The shapes and
#| colors of points represent species. Penguins from each island are on a
#| separate facet. Within each facet, the relationship between body mass and
#| flipper length is positive, linear, relatively strong.
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point(aes(color = species, shape = species)) +
facet_wrap(~island)
```
2015-11-17 10:19:27 +08:00
2022-12-05 02:05:38 +08:00
You will learn about many other geoms for visualizing distributions of variables and relationships between them in @sec-layers.
### Exercises
1. The `mpg` data frame that is bundled with the ggplot2 package contains `r nrow(mpg)` observations collected by the US Environmental Protection Agency on `r mpg |> distinct(model) |> nrow()` car models.
Which variables in `mpg` are categorical?
Which variables are numerical?
(Hint: Type `?mpg` to read the documentation for the dataset.) How can you see this information when you run `mpg`?
2022-12-05 02:05:38 +08:00
2. Make a scatterplot of `hwy` vs. `displ` using the `mpg` data frame.
Next, map a third, numerical variable to `color`, then `size`, then both `color` and `size`, then `shape`.
2022-12-05 02:05:38 +08:00
How do these aesthetics behave differently for categorical vs. numerical variables?
2016-02-11 22:17:17 +08:00
2022-12-05 02:05:38 +08:00
3. In the scatterplot of `hwy` vs. `displ`, what happens if you map a third variable to `linewidth`?
2016-07-22 05:54:19 +08:00
2022-12-05 02:05:38 +08:00
4. What happens if you map the same variable to multiple aesthetics?
2022-12-05 02:05:38 +08:00
5. Make a scatterplot of `bill_depth_mm` vs. `bill_length_mm` and color the points by `species`.
What does adding coloring by species reveal about the relationship between these two variables?
What about faceting by species?
6. Why does the following yield two separate legends?
2022-12-05 02:05:38 +08:00
How would you fix it to combine the two legends?
```{r}
2022-12-05 02:05:38 +08:00
#| warning: false
#| fig-show: hide
2022-12-05 02:05:38 +08:00
ggplot(
data = penguins,
mapping = aes(
x = bill_length_mm, y = bill_depth_mm,
color = species, shape = species
)
) +
2022-12-05 02:05:38 +08:00
geom_point() +
labs(color = "Species")
2016-02-11 22:17:17 +08:00
```
7. Create the two following stacked bar plots.
Which question can you answer with the first one?
Which question can you answer with the second one?
```{r}
#| fig-show: hide
ggplot(penguins, aes(x = island, fill = species)) +
geom_bar(position = "fill")
ggplot(penguins, aes(x = species, fill = island)) +
geom_bar(position = "fill")
```
2022-12-05 02:05:38 +08:00
## Saving your plots {#sec-ggsave}
2022-12-05 02:05:38 +08:00
Once you've made a plot, you might want to get it out of R by saving it as an image that you can use elsewhere.
That's the job of `ggsave()`, which will save the plot most recently created to disk:
2022-12-05 02:05:38 +08:00
```{r}
#| fig-show: hide
#| warning: false
2022-12-05 02:05:38 +08:00
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +
geom_point()
ggsave(filename = "penguin-plot.png")
2022-12-05 02:05:38 +08:00
```
2022-12-05 02:05:38 +08:00
```{r}
#| include: false
2023-03-09 07:28:34 +08:00
file.remove("penguin-plot.png")
2022-12-05 02:05:38 +08:00
```
This will save your plot to your working directory, a concept you'll learn more about in @sec-workflow-scripts-projects.
2022-12-05 02:05:38 +08:00
If you don't specify the `width` and `height` they will be taken from the dimensions of the current plotting device.
For reproducible code, you'll want to specify them.
You can learn more about `ggsave()` in the documentation.
2022-12-05 02:05:38 +08:00
Generally, however, we recommend that you assemble your final reports using Quarto, a reproducible authoring system that allows you to interleave your code and your prose and automatically include your plots in your write-ups.
You will learn more about Quarto in @sec-quarto.
### Exercises
2022-12-05 02:05:38 +08:00
1. Run the following lines of code.
Which of the two plots is saved as `mpg-plot.png`?
Why?
```{r}
2022-12-05 02:05:38 +08:00
#| eval: false
2022-12-05 02:05:38 +08:00
ggplot(mpg, aes(x = class)) +
geom_bar()
ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point()
ggsave("mpg-plot.png")
```
2022-12-05 02:05:38 +08:00
2. What do you need to change in the code above to save the plot as a PDF instead of a PNG?
How could you find out what types of image files would work in `ggsave()`?
2022-12-05 02:05:38 +08:00
## Common problems
2015-11-19 06:01:07 +08:00
2022-12-05 02:05:38 +08:00
As you start to run R code, you're likely to run into problems.
Don't worry --- it happens to everyone.
We have all been writing R code for years, but every day we still write code that doesn't work on the first try!
2022-12-05 02:05:38 +08:00
Start by carefully comparing the code that you're running to the code in the book.
R is extremely picky, and a misplaced character can make all the difference.
Make sure that every `(` is matched with a `)` and every `"` is paired with another `"`.
Sometimes you'll run the code and nothing happens.
Check the left-hand of your console: if it's a `+`, it means that R doesn't think you've typed a complete expression and it's waiting for you to finish it.
In this case, it's usually easy to start from scratch again by pressing ESCAPE to abort processing the current command.
2015-11-19 06:01:07 +08:00
2022-12-05 02:05:38 +08:00
One common problem when creating ggplot2 graphics is to put the `+` in the wrong place: it has to come at the end of the line, not the start.
In other words, make sure you haven't accidentally written code like this:
2015-11-19 06:01:07 +08:00
```{r}
2022-12-05 02:05:38 +08:00
#| eval: false
2022-12-05 02:05:38 +08:00
ggplot(data = mpg)
+ geom_point(mapping = aes(x = displ, y = hwy))
```
2022-12-05 02:05:38 +08:00
If you're still stuck, try the help.
You can get help about any R function by running `?function_name` in the console, or highlighting the function name and pressing F1 in RStudio.
2022-12-05 02:05:38 +08:00
Don't worry if the help doesn't seem that helpful - instead skip down to the examples and look for code that matches what you're trying to do.
2022-12-05 02:05:38 +08:00
If that doesn't help, carefully read the error message.
Sometimes the answer will be buried there!
But when you're new to R, even if the answer is in the error message, you might not yet know how to understand it.
Another great tool is Google: try googling the error message, as it's likely someone else has had the same problem, and has gotten help online.
## Summary
2022-12-05 02:05:38 +08:00
In this chapter, you've learned the basics of data visualization with ggplot2.
2022-12-05 15:46:05 +08:00
We started with the basic idea that underpins ggplot2: a visualization is a mapping from variables in your data to aesthetic properties like position, color, size and shape.
2022-12-05 02:05:38 +08:00
You then learned about increasing the complexity and improving the presentation of your plots layer-by-layer.
You also learned about commonly used plots for visualizing the distribution of a single variable as well as for visualizing relationships between two or more variables, by levering additional aesthetic mappings and/or splitting your plot into small multiples using faceting.
We'll use visualizations again and again throughout this book, introducing new techniques as we need them as well as do a deeper dive into creating visualizations with ggplot2 in @sec-layers through @sec-communication.
With the basics of visualization under your belt, in the next chapter we're going to switch gears a little and give you some practical workflow advice.
2022-12-16 22:22:06 +08:00
We intersperse workflow advice with data science tools throughout this part of the book because it'll help you stay organized as you write increasing amounts of R code.