Skip to content

Commit 8a7e48d

Browse files
authored
Merge pull request #826 from matthieu-bruneaux/fix-merge-conflicts-PR-552
2 parents cd54eff + 98a3f70 commit 8a7e48d

File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed

_episodes_rmd/08-plot-ggplot2.Rmd

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,53 +40,67 @@ it is the most effective for creating publication-quality
4040
graphics.
4141

4242
ggplot2 is built on the grammar of graphics, the idea that any plot can be
43-
expressed from the same set of components: a **data** set, a
44-
**coordinate system**, and a set of **geoms** -- the visual representation of data
45-
points.
46-
47-
The key to understanding ggplot2 is thinking about a figure in layers.
48-
This idea may be familiar to you if you have used image editing programs like Photoshop, Illustrator, or
49-
Inkscape.
50-
51-
Let's start off with an example:
43+
built from the same set of components: a **data set**,
44+
**mapping aesthetics**, and graphical **layers**:
45+
46+
* **Data sets** are the data that you, the user, provide.
47+
48+
* **Mapping aesthetics** are what connect the data to the graphics.
49+
They tell ggplot2 how to use your data to affect how the graph looks,
50+
such as changing what is plotted on the X or Y axis, or the size or
51+
color of different data points.
52+
53+
* **Layers** are the actual graphical output from ggplot2. Layers
54+
determine what kinds of plot are shown (scatterplot, histogram, etc.),
55+
the coordinate system used (rectangular, polar, others), and other
56+
important aspects of the plot. The idea of layers of graphics may
57+
be familiar to you if you have used image editing programs
58+
like Photoshop, Illustrator, or Inkscape.
59+
60+
Let's start off building an example using the gapminder data from earlier.
61+
The most basic function is `ggplot`, which lets R know that we're
62+
creating a new plot. Any of the arguments we give the `ggplot`
63+
function are the *global* options for the plot: they apply to all
64+
layers on the plot.
5265

53-
```{r lifeExp-vs-gdpPercap-scatter, message=FALSE}
66+
```{r blank-ggplot, message=FALSE, fig.alt = "Blank plot, before adding any mapping aesthetics to ggplot()."}
5467
library("ggplot2")
55-
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
56-
geom_point()
68+
ggplot(data = gapminder)
5769
```
5870

59-
So the first thing we do is call the `ggplot` function. This function lets R
60-
know that we're creating a new plot, and any of the arguments we give the
61-
`ggplot` function are the *global* options for the plot: they apply to all
62-
layers on the plot.
63-
64-
We've passed in two arguments to `ggplot`. First, we tell `ggplot` what data we
65-
want to show on our figure, in this example the gapminder data we read in
66-
earlier. For the second argument, we passed in the `aes` function, which
67-
tells `ggplot` how variables in the **data** map to *aesthetic* properties of
68-
the figure, in this case the **x** and **y** locations. Here we told `ggplot` we
69-
want to plot the "gdpPercap" column of the gapminder data frame on the x-axis, and
70-
the "lifeExp" column on the y-axis. Notice that we didn't need to explicitly
71-
pass `aes` these columns (e.g. `x = gapminder[, "gdpPercap"]`), this is because
72-
`ggplot` is smart enough to know to look in the **data** for that column!
71+
Here we called `ggplot` and told it what data we want to show on
72+
our figure. This is not enough information for `ggplot` to actually
73+
draw anything. It only creates a blank slate for other elements
74+
to be added to.
7375

74-
By itself, the call to `ggplot` isn't enough to draw a figure:
76+
Now we're going to add in the **mapping aesthetics** using the
77+
`aes` function. `aes` tells `ggplot` how variables in the **data**
78+
map to *aesthetic* properties of the figure, such as which columns
79+
of the data should be used for the **x** and **y** locations.
7580

76-
```{r, fig.alt = "Plotting area with axes for a scatter plot of life expectancy vs GDP with no data points visible."}
81+
```{r ggplot-with-aes, message=FALSE, fig.alt = "Plotting area with axes for a scatter plot of life expectancy vs GDP, with no data points visible."}
7782
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp))
7883
```
7984

80-
We need to tell `ggplot` how we want to visually represent the data, which we
81-
do by adding a new **geom** layer. In our example, we used `geom_point`, which
82-
tells `ggplot` we want to visually represent the relationship between **x** and
83-
**y** as a scatterplot of points:
85+
Here we told `ggplot` we want to plot the "gdpPercap" column of the
86+
gapminder data frame on the x-axis, and the "lifeExp" column on the
87+
y-axis. Notice that we didn't need to explicitly pass `aes` these
88+
columns (e.g. `x = gapminder[, "gdpPercap"]`), this is because
89+
`ggplot` is smart enough to know to look in the **data** for that column!
90+
91+
The final part of making our plot is to tell `ggplot` how we want to
92+
visually represent the data. We do this by adding a new **layer**
93+
to the plot using one of the **geom** functions.
8494

85-
```{r lifeExp-vs-gdpPercap-scatter2, fig.alt = "Scatter plot of life expectancy vs GDP per capita, showing a positive correlation between the two variables with data points added."}
95+
```{r lifeExp-vs-gdpPercap-scatter, message=FALSE, fig.alt = "Scatter plot of life expectancy vs GDP per capita, now showing the data points."}
8696
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
8797
geom_point()
8898
```
8999

100+
Here we used `geom_point`, which tells `ggplot` we want to visually
101+
represent the relationship between **x** and **y** as a scatterplot of points.
102+
103+
90104
> ## Challenge 1
91105
>
92106
> Modify the example so that the figure shows how life expectancy has

0 commit comments

Comments
 (0)