Aesthetic mappings
Introduction
In this worksheet, we will discuss a core concept of ggplot, the mapping of data values onto aesthetics.
First we need to load the required R packages. Please wait a moment until the live R session is fully set up and all packages are loaded.
Next we set up the data.
We will first work with the dataset temps_houston
which contains the average temperature for each day of the year for Houston, TX.
Basic use of ggplot
In the most basic use of ggplot, we call the ggplot()
function with a dataset and an aesthetic mapping (created with aes()
), and then we add a geom, such as geom_line()
to draw lines or geom_point()
to draw points.
Try this for yourself. Map the column day_of_year
onto the x axis and the column temperature
onto the y axis, and use geom_line()
to display the data.
ggplot(temps_houston, aes(x = day_of_year, y = temperature)) +
geom_line()
Try again. Now use geom_point()
instead of geom_line()
.
ggplot(temps_houston, aes(x = day_of_year, y = temperature)) +
geom_point()
And now swap which column you map to x and which to y.
ggplot(temps_houston, aes(x = temperature, y = day_of_year)) +
geom_point()
More complex geoms
You can use other geoms to make different types of plots. For example, geom_boxplot()
will make boxplots. For boxplots, we frequently want categorical data on the x or y axis. For example, we might want a separate boxplot for each month. Try this out. Puth month
on the x axis, temperature
on the y axis, and use geom_boxplot()
.
ggplot(temps_houston, aes(x = month, y = temperature)) +
___()
ggplot(temps_houston, aes(x = month, y = temperature)) +
geom_boxplot()
Now put the month on the y axis and the temperature on the x axis.
ggplot(temps_houston, aes(x = ___, y = ___)) +
geom_boxplot()
ggplot(temps_houston, aes(x = temperature, y = month)) +
geom_boxplot()
Adding color
Next we will be working with the dataset temperatures
, which is similar to temps_houston
but contains data for three more locations.
Make a line plot of temperature
against day_of_year
, using the color
aesthetic to color the lines by location.
ggplot(temperatures, aes(x = day_of_year, y = temperature, color = ___)) +
geom_line()
ggplot(temperatures, aes(x = day_of_year, y = temperature, color = location)) +
geom_line()
Try again, this time using location
as the location along the y axis and temperature
for the color. This plot looks better with geom_point()
than geom_line()
. (Try it out to see why. Also, try geom_point(size = 5)
to create larger points.)
ggplot(temperatures, aes(x = ___, y = ___, color = ___)) +
geom_point()
ggplot(temperatures, aes(x = day_of_year, y = location, color = temperature)) +
geom_point()
Using the fill
aesthetic
Some geoms use a fill
aesthetic, which is similar to color
but applies to shaded areas. (color
applies to lines and points.) For example, we can use the fill
aesthetic with geom_boxplot()
to color the interior of the box. Try this yourself. Plot month
on x, temperature
on y, and color the interior of the box by location.
ggplot(temperatures, aes(x = month, y = ___, fill = ___)) +
geom_boxplot()
ggplot(temperatures, aes(x = month, y = temperature, fill = location)) +
geom_boxplot()
Can you color the lines of the boxplot by location and the interior by month? Try it.
ggplot(temperatures, aes(x = month, y = temperature, color = ___, fill = ___)) +
geom_boxplot()
ggplot(temperatures, aes(x = month, y = temperature, color = location, fill = month)) +
geom_boxplot()
Using aesthetics as parameters
Many of the aesthetics (such as color
, fill
, and also size
to change line size or point thickness) can be used as parameters inside a geom rather than inside an aes()
statement. The difference is that when you use an aesthetic as a parameter, you specify a specific value, such as color = "blue"
, rather than a mapping, such as aes(color = location)
. Notice the difference: Inside the aes()
function, we don’t actually specify the specific color values, ggplot does that for us. We only say that we want the data values of the location
column to correspond to different colors. (We will learn later how to tell ggplot to use specific colors in this mapping.)
Try this with the boxplot example from the previous section. Map location
onto the fill
aesthetic but set the color of the lines to "navyblue"
.
ggplot(temperatures, aes(x = month, y = temperature, fill = ___)) +
geom_boxplot(color = ___)
ggplot(temperatures, aes(x = month, y = temperature, fill = location)) +
geom_boxplot(color = "navyblue")
Now do the reverse. Map location
onto the line colors but fill the box with the color "navyblue"
.
ggplot(temperatures, aes(x = month, y = temperature, color = ___)) +
geom_boxplot(fill = ___)
ggplot(temperatures, aes(x = month, y = temperature, color = location)) +
geom_boxplot(fill = "navyblue")