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
Plot Labels
This lesson is locked
This lesson is called Plot Labels, part of the Fundamentals of R course. This lesson is called Plot Labels, 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")
# 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
You need to be signed-in to comment on this post. Login.
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 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.
Rachel Udow
March 29, 2024
Thank you, Libby - this is super helpful!