Plot Labels
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.
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!
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.
Rachel Udow • March 26, 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 26, 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!