Skip to content
R for the Rest of Us: A Statistics-Free Introduction comes out June 25th. Or you can read the online version today. Check it out →
R for the Rest of Us Logo

This lesson is locked

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

Have any questions? Put them below and we will help you out!

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

Maria Montenegro

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

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.