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.
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.
Have any questions? Put them below and we will help you out!
Course Content
34 Lessons
1
The Grammar of Graphics
04:39
2
Scatterplots
03:46
3
Histograms
05:47
4
Bar Charts
06:37
5
Setting color and fill Aesthetic Properties
02:39
6
Setting color and fill Scales
05:40
7
Setting x and y Scales
03:09
8
Adding Text to Plots
07:32
9
Plot Labels
03:57
10
Themes
02:19
11
Facets
03:12
12
Save Plots
02:57
13
Bring it All Together (Data Visualization)
06:42
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.