Hello! Here are some paragraphs showing the kind of example density that I think is a hallmark of good tutorials.

The idea is to move quickly between examples, rather than spend too much time explaining single examples.

The analogy to math education is instructive. Think of how you would teach a child long division—lots and lots of examples, and very little theoretical explication of what it means to “carry over” or what it means to have a remainder.

(A definition like “the remainder is the value that is left over after subtracting the divisor as many times as possible from the dividend without producing a negative result” would not be helpful to someone who is learning long division. It may be helpful to experts writing theorems or programs that involve division, but for the student that is naive to the topic, it is much more helpful to show them what a reminder is than to try to define or explain it.)


START OF TUTORIAL SAMPLE

To make clear the difference between mapping and fixing aesthetics, we’ll go through a few examples, using the following simple scatter plot showing the GDP (per capita) and average life expectancy in a range of countries:

ggplot(country_data) + 
  geom_point(aes(x = gdp, y = life_expectancy))

[Note to teacher: Here, resist the urge to spend time explaining the meaning/significance of the trend seen. Let the student consider that in their spare time. You can add one or two sentences of explanation, but ideally no more than that. Conciseness is more valuable than the teacher’s economic ruminations in this context.]

Mapping vs fixing: point color

Let’s start with the color aesthetic.

Below we map the color aesthetic to the life_expectancy variable; the result is that the color of the points changes as the life_expectancy values change.

ggplot(country_data) +
   geom_point(aes(x = gdp, y = life_expectancy, color = life_expectancy))

Note how, in the code above, the color argument is given inside the aes() function.

Alternatively, we could fix the color aesthetic to a single value, say “blue”:

ggplot(country_data) +
   geom_point(aes(x = gdp, y = life_expectancy), color = "blue")

In this latter case, the color = "blue" argument is outside the aes() function all points are colored the same; the color aesthetic stays “fixed”.

Another example: below, we map the color aesthetic to the continent variable, such that points from each continent take on a different color:

ggplot(country_data) +
   geom_point(aes(x = gdp, y = life_expectancy, color = continent))

Note again, that color is supplied inside of the aes() function.

And, to emphasize the mapping vs fixing distinction, here is an another example where we fix the color aesthetic to the color “firebrick”:

ggplot(country_data) +
   geom_point(aes(x = gdp, y = life_expectancy), color = "firebrick")

Now, here is one last example. Can you tell whether it is an example of mapping an aesthetic, or fixing an aesthetic?

ggplot(country_data) +
   geom_point(aes(x = gdp, y = life_expectancy, color = population))

If you thought “mapping”, you are correct! Here, color is mapped to the population variable, such that countries with larger populations are lighter-colored.

(Two very populous countries, India and China, stand out here, making the rest of the colors hard to distinguish. Later, you will see how to modify the color scale to better show the country-to-country variation in a example like this.)

Practice:

Consider the data below, which shows the daily values of some weather measurements in New York.

new_york_weather <- airquality %>%
  transmute(day = Day,
            month = lubridate::month(Month, label = T),
            temp_fahrenheit = Temp,
            ozone_conc = Ozone,
            wind_speed = Wind)

head(new_york_weather)
##   day month temp_fahrenheit ozone_conc wind_speed
## 1   1   May              67         41        7.4
## 2   2   May              72         36        8.0
## 3   3   May              74         12       12.6
## 4   4   May              62         18       11.5
## 5   5   May              56         NA       14.3
## 6   6   May              66         28       14.9

Modify the code below to create a scatterplot of this data in which the color aesthetic is mapped to the wind_speed variable.

ggplot(new_york_weather) + 
  geom_point(aes(x = temp_fahrenheit, y = ozone_conc))

Now, modify the code again to create a scatterplot in which the color aesthetic is fixed to the value “red”:

ggplot(new_york_weather) + 
  geom_point(aes(x = temp_fahrenheit, y = ozone_conc))

Mapping vs fixing: point size

Similar to point color, we can also either map point size to a variable or fix it to a single value.

Consider the following examples:

ggplot(country_data) +
   geom_point(aes(x = gdp, y = life_expectancy, size = population)) # MAP size to population

ggplot(country_data) +
   geom_point(aes(x = gdp, y = life_expectancy, size = gdp)) # MAP size to gdp

ggplot(country_data) +
   geom_point(aes(x = gdp, y = life_expectancy), size = 1) # FIX size to size 1

ggplot(country_data) +
   geom_point(aes(x = gdp, y = life_expectancy), size = 4) # FIX size to size 4

Do you see the difference? When we map the size aesthetic, the point sizes change in proportion to the select variables, but when we fix the aesthetic, the point size is permanently changed.

Practice

Consider the xxx dataset shown below.

Modify the code to map the size aesthetic of the points to the xxx variable

Now, modify the code to fix the size size aesthetic to the value 5.

Mapping vs fixing: point shape

Finally, let’s look at mapping vs fixing in the context of the shape aesthetic.

Here, you will see that while size and color can be mapped to both continuous and discrete variables, shape can only be mapped to discrete ones.

[Note to teacher: here would be an opportunity to practice the show don’t tell principle. You should actually show them what will happen if you try to map shape to a continuous variable, for example, GDP, or population—namely, this error : ! A continuous variable can not be mapped to shape. You would want to show this both in the lesson text and in the lesson video.]