Get access to all lessons in this course.
- Welcome to Fundamentals of R
- Update Everything
- Start a New Project
-
Data Wrangling and Analysis
- The Tidyverse
- Pipes
- select()
- mutate()
- filter()
- summarize()
- group_by() and summarize()
- arrange()
- Create a New Data Frame
- Bring it All Together (Data Wrangling)
-
Data Visualization
- The Grammar of Graphics
- Scatterplots
- Histograms
- Bar Charts
- Setting color and fill Aesthetic Properties
- Setting color and fill Scales
- Setting x and y Scales
- Adding Text to Plots
- Plot Labels
- Themes
- Facets
- Save Plots
- Bring it All Together (Data Visualization)
-
Quarto
- Quarto Overview
- YAML
- Text
- Code Chunks
- Tips for Working with Quarto
- Bring It All Together (Quarto)
-
Wrapping Up
- An Important Workflow Tip
Fundamentals of R
Setting color and fill Aesthetic Properties
This lesson is locked
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.
If the video is not playing correctly, you can watch it in a new window
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.
You need to be signed-in to comment on this post. Login.