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.
View code shown in video
# Load Packages -----------------------------------------------------------
library(tidyverse)
# Import Data -------------------------------------------------------------
penguins <- read_csv("penguins.csv")
# 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
34 Lessons
You need to be signed-in to comment on this post. Login.
Tony Yu • March 19, 2025
Why is it that the color= option is treated differently by geom_point and geom_col? The logic seems inconsistent here since "color=" is used as if it is "fill =" by geom_point. What if I want the scatter plot to have black dots with outline color different by island?
Gracielle Higino Coach • March 21, 2025
Hi Tony! That's a great observation! Different geoms have different geometric properties - some are solid points and lines, others can be filled in. The default for geom_point is to use a kind of shape that does not have a filling, but you can change that using the argument
shape
inside yourgeom_point()
layer. Theshape
argument has some options that are not "fillable" and others that are. This figure shows the different options of "point shapes".Play with the code below to explore these options:
In the code above I specified a filling color and a contour color in the aes layer, determined the shape of the point that I wanted (
shape = 21
), the size of each point and the contour line width.Let me know if that's clearer now!