Setting color and fill Aesthetic Properties
This lesson is called Setting color and fill Aesthetic Properties, part of the Fundamentals of R course. This lesson is called Setting color and fill Aesthetic Properties, part of the Fundamentals of R course.
Transcript
Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.
Loading transcript...
View code shown in video
# Load Packages -----------------------------------------------------------
library(tidyverse)
# Import Data -------------------------------------------------------------
penguins <- read_csv("penguins.csv")
penguin_bill_length_by_island <-
penguins |>
group_by(island) |>
summarize(mean_bill_length = mean(bill_length_mm, na.rm = TRUE)) |>
arrange(mean_bill_length)
# Setting color and fill Aesthetic Properties -----------------------------
# We add the color argument within aes() so that
# the data in that variable is mapped to those aesthetic properties.
# With this code, the island variable is mapped to the aesthetic property color
ggplot(
data = penguins,
mapping = aes(
x = bill_length_mm,
y = bill_depth_mm,
color = island
)
) +
geom_point()
# Let's try the same thing with our bar chart
ggplot(
data = penguin_bill_length_by_island,
mapping = aes(
x = island,
y = mean_bill_length,
color = island
)
) +
geom_col()
# That didn't work! Let's try fill instead.
ggplot(
data = penguin_bill_length_by_island,
mapping = aes(
x = island,
y = mean_bill_length,
fill = island
)
) +
geom_col()
Your Turn
# Load Packages -----------------------------------------------------------
library(tidyverse)
# Import Data -------------------------------------------------------------
penguins <- read_csv("penguins.csv")
# Setting color and fill Aesthetic Properties -----------------------------
# Take your graph that uses geom_col() and make the inside of each bar a different color.
# YOUR CODE HERE
# Make your scatterplot from before with flipper length on the x axis and body mass on the y axis
# but make the points different colors based on the island variable
# YOUR CODE HERE
Learn More
You may want to review Chapter 11 from R for Data Science, which includes a section on mapping data to color and fill aesthetics. Chapter 2 of Fundamentals of Data Visualization has a similar discussion. So does Chapter 3 of Data Visualization: A Practical Introduction.
Chapter 11 of ggplot2: Elegant Graphics for Data Analysis is also a good place to learn about color and fill scales.
Have any questions? Put them below and we will help you out!
Course Content
33 Lessons
You need to be signed-in to comment on this post. Login.
J.R. Moller • March 23, 2026
For both graphics, I had to include "island" with the quotes for it to work. Additionally, it didn't default to the three colors but instead did a single color for the fill. I'm not sure what I did wrong. Here's my code for the scatterplot: ggplot( data = penguins, mapping = aes( x = flipper_length_mm, y = body_mass_g, color = "island" ) ) + geom_point()
J.R. Moller • March 23, 2026
My version of R (4.5.3) crashed and errored out. When it rebooted, everything seems to work properly now. Please disregard my last comment (and this one, too).