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
Themes
This lesson is locked
This lesson is called Themes, part of the Fundamentals of R course. This lesson is called Themes, 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")
# Themes ------------------------------------------------------------------
# To add a theme to a plot, we use the theme_ set of functions.
# There are several built-in themes. For instance, theme_minimal().
ggplot(data = penguin_bill_length_by_island_and_sex,
mapping = aes(x = island,
y = mean_bill_length,
fill = sex)) +
geom_col(position = "dodge") +
labs(title = "Males have longer bills than females",
subtitle = "But they're all good penguins",
caption = "Data from the palmerpenguins R package",
x = NULL,
y = "Mean Bill Length in Millimeters",
fill = NULL) +
theme_minimal()
# There's also theme_light().
ggplot(data = penguin_bill_length_by_island_and_sex,
mapping = aes(x = island,
y = mean_bill_length,
fill = sex)) +
geom_col(position = "dodge") +
labs(title = "Males have longer bills than females",
subtitle = "But they're all good penguins",
caption = "Data from the palmerpenguins R package",
x = NULL,
y = "Mean Bill Length in Millimeters",
fill = NULL) +
theme_light()
# There are also packages that give you themes you can apply to your plots.
# Let's load the ggthemes() package
# Install it if necessary using install.packages("ggthemes")
library(ggthemes)
# We can then use a theme from this package called theme_economist()
# to make our plots look like those in the Economist.
ggplot(data = penguin_bill_length_by_island_and_sex,
mapping = aes(x = island,
y = mean_bill_length,
fill = sex)) +
geom_col(position = "dodge") +
labs(title = "Males have longer bills than females",
subtitle = "But they're all good penguins",
caption = "Data from the palmerpenguins R package",
x = NULL,
y = "Mean Bill Length in Millimeters",
fill = NULL) +
theme_economist()
# You can see a number of other themes from this package at
# https://yutannihilation.github.io/allYourFigureAreBelongToUs/ggthemes/
Your Turn
# Load Packages -----------------------------------------------------------
library(tidyverse)
# Import Data -------------------------------------------------------------
penguins <- read_csv("penguins.csv")
# Themes ------------------------------------------------------------------
# Use one of the built-in ggplot2 themes to change the look and feel of your last plot
# https://ggplot2.tidyverse.org/reference/index.html#themes
# YOUR CODE HERE
# Install the ggthemes package
# Load the ggthemes package
# Use one of themes from the package to update your last plot
# The themes can be found here:
# https://yutannihilation.github.io/allYourFigureAreBelongToUs/ggthemes/
# YOUR CODE HERE
Learn More
Data Visualization: A Practical Introduction has a section on themes in Chapter 8.
If you’re looking for packages that give you extra themes, check out this roundup.
Note that theme packages often include code that changes to overall look and feel as well as palettes that you can apply to the color and fill scales.
You need to be signed-in to comment on this post. Login.
Maria Montenegro
April 9, 2024
This is super helpful! I'm assuming we can create our own themes? Would you be able to show us how to go about this?
David Keyes Founder
April 9, 2024
Yes! In the Going Deeper Advanced Data Visualization section, there are lessons that show you how to make your own custom ggplot theme.