Set-up
We'll load the full {tidyverse}
and {palmerpenguins}
for the penguins
data set.
library(tidyverse)
library(palmerpenguins)
Geometric object properties
Often we want to adjust aspects of the geometric object properties (e.g., color, fill, size, shape, transparency) to change the appearance of our plots. In this case, let's change the color of our points. To do that, we set the color
argument inside the geom_point()
function.
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point(color = "seagreen")
Now, in addition to making the points seagreen, set the symbol shape to filled triangles, the size to 3, and the transparency to 0.75 to match this plot:
penguins |>
...
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point(color = "seagreen", shape = 17, size = 3, alpha = 0.75)
Mapping aesthetics
While the previous examples changed geometric object properties for the whole plot, sometimes we want to map data onto properties of our plots. These aesthetic mappings must be defined in an aes()
function.
First, let's recreate our bill length/depth plot but color the individual data points based on the penguin species.
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
geom_point()
Notice that just adding color as an aesthetic and mapping it to species both specifies different colors for different levels of species and automatically creates a legend.
Now create a version of the plot that includes both different colors and different symbols for the different species to generate this plot:
penguins |>
...
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm, color = species, shape = species)) +
geom_point()
Controlling properties across multiple geometric objects
We can now combine the last several sections to control object properties across multiple geometric objects. There are three ways to control object properties:
- Properties are not mapped to data and therefore apply to all plot elements within a geometric object (e.g., every point)
- Properties are mapped to data and apply to all geometric objects (e.g, points and lines)
- Properties are mapped to data but apply to a subset of geometric objects (e.g., points or lines)
As we have already seen, non-mapped properties are defined in the geometric object function (e.g., geom_point()
). Mapped aesthetic properties can either apply globally to all geometric objects in the plot or locally to specific geometric objects. To map aesthetics globally, include them in an aes()
function in the ggplot()
function at the top of the plot. To map them locally, include them in an aes()
function in the individual geometric object functions (e.g., geom_point()
). These can be mixed in the same plot (e.g., some aesthetics mapped globally and others locally).
Non-mapped properties
Let's start with multiple geometric objects where we define the non-mapped object property color to all geoms. To do this, we must include the color argument in all geoms that we want it to apply to.
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point(color = "darkorchid") +
geom_smooth(method = "lm", color = "darkorchid")
Change the color of all geoms to be tomato.
penguins |>
...
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point(color = "tomato") +
geom_smooth(method = "lm", color = "tomato")
Globally mapped properties
For mapped aesthetic properties, we need to include the property argument in the aes()
function that is in the ggplot()
function (e.g., where x
and y
properties are defined).
Remove the color arguments from the geoms in the previous code. Then define color based on species in the top-level aesthetic function to generate this plot:
penguins |>
...
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm, color = species)) +
geom_point() +
geom_smooth(method = "lm")
Notice how putting color in the top-level aesthetic not only used different colors for the three species' points but also created three separate regression lines---one for each species. Moreover, both the point and the regression line colors were mapped to the species. This is because we globally mapped the aesthetic color.
Locally mapped properties
What if we want to map the aesthetics differently in different geoms? First, let's go back to having a single regression line rather than one per species. But let's keep the separate colors of points for different species. This means that we will not use global aesthetics. Instead, we will map color to species in the points and have no mapped aesthetics for the regression line.
Move the color aesthetic mapped to species from the top-level to the geom_point()
to generate this plot:
penguins |>
...
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm)) +
geom_point(aes(color = species)) +
geom_smooth(method = "lm")
Grouping without changing properties
Let's say that we like the plot with separate regression lines for each species, but we don't want the line colors to be different for each species. We can group the data based on species but not change any of the properties of the regression lines, while still allowing the data point colors to map to species.
To generate this plot, add group = species
to the top-level aesthetic, add color = species
to the geom_point()
aesthetic, and no aesthetics in the geom_smooth()
.
penguins |>
...
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm, group = species)) +
geom_point(aes(color = species)) +
geom_smooth(method = "lm")
I don't like having the blue regression lines because they blend in with the gentoo penguin data.
Replicate the previous plot, but make the regression lines black.
penguins |>
...
penguins |>
ggplot(aes(x = bill_length_mm, y = bill_depth_mm, group = species)) +
geom_point(aes(color = species)) +
geom_smooth(method = "lm", color = "black")
Wrap-up
Congratulations, you finished the tutorial!
To get credit for this assignment, replace my name with the first name that you submitted in the course introduction form in the code below and click Run Code to generate the text for you to submit to Canvas.
# replace my name below with your first name (surrounded by quotes)
first_name <- "Jeff"
generate_text(first_name)
Assignment complete!
Great! Copy that code into Canvas, and you're all set for this tutorial.