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.
Loading transcript...
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
33 Lessons
1
The Grammar of Graphics
04:36
2
Scatterplots
03:40
3
Histograms
04:51
4
Bar Charts
04:53
5
Setting color and fill Aesthetic Properties
02:43
6
Setting color and fill Scales
05:12
7
Setting x and y Scales
02:58
8
Adding Text to Plots
05:50
9
Plot Labels
02:59
10
Themes
02:10
11
Facets
02:56
12
Save Plots
02:49
13
Bring it All Together (Data Visualization)
06:14
You need to be signed-in to comment on this post. Login.