Skip to content
R for the Rest of Us Logo

Setting color and fill Aesthetic Properties

This lesson is locked

Get access to all lessons in this 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!

You need to be signed-in to comment on this post. Login.