Skip to content
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")
			
# Plot Labels -------------------------------------------------------------

# To start, let's make a new data frame

penguin_bill_length_by_island_and_sex <- penguins |> 
  drop_na(sex) |> 
  group_by(island, sex) |> 
  summarize(mean_bill_length = mean(bill_length_mm))

# Now let's plot this data frame using a bar chart.

ggplot(data = penguin_bill_length_by_island_and_sex,
       mapping = aes(x = island,
                     y = mean_bill_length,
                     fill = sex)) +
  geom_col()

# The bars are stacked by default. 
# To put them side by side, we use the 
# position = "dodge" argument within geom_col().

ggplot(data = penguin_bill_length_by_island_and_sex,
       mapping = aes(x = island,
                     y = mean_bill_length,
                     fill = sex)) +
  geom_col(position = "dodge")

# To add labels to our plot, we use labs().

# We can add a title to the plot with the title argument.

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")

# We can also add a subtitle and caption

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")

# We can change the x and y axis labels using the x and y arguments.

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 = "Island",
       y = "Mean Bill Length in Millimeters")

# To change the legend title, 
# we use the name of the aesthetic that is being shown.

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 = "Island",
       y = "Mean Bill Length in Millimeters",
       fill = "Sex")

# You can remove plot labels using NULL

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)

Your Turn

# Load Packages -----------------------------------------------------------

library(tidyverse)

# Import Data -------------------------------------------------------------

penguins <- read_csv("penguins.csv")

# Plot Labels -------------------------------------------------------------

# Copy the code for the last plot you made that uses geom_label().
# Then do the following:
# 1. Add a title
# 2. Remove the x and y axis labels

# YOUR CODE HERE

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

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

Rachel Udow

Rachel Udow

March 27, 2024

There are a lot of functions involved in data visualization! Are there widely accepted best practices for how to order them?

Libby Heeren

Libby Heeren Coach

March 27, 2024

ggplot2 (and the grammar of graphics in general) works by layering, so that's the most important concept to keep in mind. The things you add underneath can cover up or overwrite things you put above. We tend to do things in order of foundational (like our ggplot call, plus our geom, which gives us the background of our plot and our line/bar/points), to more mid-grain details like labels in the middle, and then super specific adjustment like removing the legend or moving the plot title at the end.

Here's an example. You'll see the order here is ggplot, geom, axis labels, setting specific limits for the y axis, and then changing a theme setting.

avg_hwy |> 
  ggplot(aes(x = year, 
           y = mean_hwy, 
           color = drive)) +
  geom_line(linewidth = 0.75) +
  labs(title = "Average Highway Gas Mileage",
       x = NULL,
       y = NULL,
       color = "Year") +
  lims( y = c(18, 22)) +
  theme_minimal(base_size = 16)

Rachel Udow

Rachel Udow

March 29, 2024

Thank you, Libby - this is super helpful!