Themes
This lesson is called Themes, part of the Fundamentals of R course. This lesson is called Themes, 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_and_sex <-
penguins |>
drop_na(sex) |>
group_by(island, sex) |>
summarize(mean_bill_length = mean(bill_length_mm))
# 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.
Have any questions? Put them below and we will help you out!
Course Content
33 Lessons
1
The Grammar of Graphics
04:36
2
Scatterplots
03:40
3
Histograms
04:51
4
Bar Charts
04:53
5
Setting color and fill Aesthetic Properties
02:43
6
Setting color and fill Scales
05:12
7
Setting x and y Scales
02:58
8
Adding Text to Plots
05:50
9
Plot Labels
02:59
10
Themes
02:10
11
Facets
02:56
12
Save Plots
02:49
13
Bring it All Together (Data Visualization)
06:14
You need to be signed-in to comment on this post. Login.
Felipe Coelho • March 26, 2026
For some reason, when I run the code shown in the video, my Positron does not recognize the line "theme_economist()".
Gracielle Higino Coach • March 26, 2026
Hi Felipe! Do you see an error message or just don't see the theme applied? If you don't see the theme being applied, check the order of your layers. If the theme_economist is applied first and then you modify your theme with the theme() function, some properties may be overwritten. If you see an error message, please paste it below (ideally along with your code!). Also double check for extra
+around your code! They are sneaky!Felipe Coelho • March 26, 2026
Thank you for the message, Gracielle. I found the problem. I had not installed ggthemes beforehand. Now I did and loaded the library, and everything is working. Thank you!
Gracielle Higino Coach • March 26, 2026
Awesome! 🎉