Adding Text to Plots
This lesson is called Adding Text to Plots, part of the Fundamentals of R course. This lesson is called Adding Text to Plots, 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")
# Adding Text to Plots ---------------------------------------------------------
# Text is just another geom.
# We can use geom_text() to add labels to our figures.
ggplot(data = penguin_bill_length_by_island,
mapping = aes(x = island,
y = mean_bill_length,
fill = island,
label = mean_bill_length)) +
geom_col() +
geom_text()
# Those text labels are too long!
# Let's create a new variable to use for plotting.
# We're using the number() function from the scales package
# to make this variable
library(scales)
penguin_bill_length_by_island_v2 <- penguin_bill_length_by_island |>
mutate(mean_bill_length_one_digit = number(mean_bill_length, accuracy = 0.1))
# Now let's plot using our new data frame
ggplot(data = penguin_bill_length_by_island_v2,
mapping = aes(x = island,
y = mean_bill_length,
fill = island,
label = mean_bill_length_one_digit)) +
geom_col() +
geom_text()
# Note that we use mean_bill_length_one_digit for the label aesthetic property
# and mean_bill_length for y.
# If you use mean_bill_length_one_digit for both, your graph will
# look different.
ggplot(data = penguin_bill_length_by_island_v2,
mapping = aes(x = island,
y = mean_bill_length_one_digit,
fill = island,
label = mean_bill_length_one_digit)) +
geom_col() +
geom_text()
# We can use the hjust and vjust arguments to horizontally and vertically
# adjust text.
# vjust = 0 puts the labels on the outer edge of the bars.
ggplot(data = penguin_bill_length_by_island_v2,
mapping = aes(x = island,
y = mean_bill_length,
fill = island,
label = mean_bill_length_one_digit)) +
geom_col() +
geom_text(vjust = 0)
# vjust = 1 puts the labels at the inner edge of the bars.
ggplot(data = penguin_bill_length_by_island_v2,
mapping = aes(x = island,
y = mean_bill_length,
fill = island,
label = mean_bill_length_one_digit)) +
geom_col() +
geom_text(vjust = 1)
# I often do something like vjust = 1.5 to give a bit more padding.
ggplot(data = penguin_bill_length_by_island_v2,
mapping = aes(x = island,
y = mean_bill_length,
fill = island,
label = mean_bill_length_one_digit)) +
geom_col() +
geom_text(vjust = 1.5)
# We can adjust the color of the text using the color argument.
# We're putting it outside of the aes() because we are setting it
# for the whole layer.
ggplot(data = penguin_bill_length_by_island_v2,
mapping = aes(x = island,
y = mean_bill_length,
fill = island,
label = mean_bill_length_one_digit)) +
geom_col() +
geom_text(vjust = 1.5,
color = "white")
# geom_label() is nearly identical but it adds a background.
# With geom_label() the color argument determines the text and border color
# while the fill is the background color.
ggplot(data = penguin_bill_length_by_island_v2,
mapping = aes(x = island,
y = mean_bill_length,
fill = island,
label = mean_bill_length_one_digit)) +
geom_col() +
geom_label(vjust = 1.5,
color = "white",
fill = "black")
Your Turn
# Load Packages -----------------------------------------------------------
library(tidyverse)
# Import Data -------------------------------------------------------------
penguins <- read_csv("penguins.csv")
# Adding Text to Plots ---------------------------------------------------------
# Copy your last code chunk.
# Then add text labels on the top of each bar that show the number of penguins of each species.
# You'll need to use geom_text() and the vjust argument to do this.
# Make the text labels show up in red.
# YOUR CODE HERE
# Do the same thing, but use geom_label() instead of geom_text().
# This time, make the text itself show up in white.
# YOUR CODE HERE
Learn More
Data Visualization: A Practical Introduction has a section in Chapter 5 on adding text to plots, as does Chapter 11 of R for Data Science.
Information about using vjust and hjust is on the geom_label page of the tidyverse website.
Also, check out the ggrepel package , which automatically adjusts overlapping text and labels.
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.
Ashley Robinson • March 28, 2024
When I added this text, my legend also got a little "a" in each color box that matched the label. Can you explain why and how to remove?
Libby Heeren Coach • March 28, 2024
Hi, Ashley! I'm sure you were with us when we talked about it in live session, but I wanted to answer here, too! To get rid of it, the method I use is to add an argument inside my
geom_text()
function that removes our text geom from being included in the legend. It looks like this:As for why it's popping up in your particular code and not in David's here, it might depend on what exactly you've got in your code! Feel free to share it and we can figure it out 🤔
John LeMay • October 27, 2024
The a's were in David's as well.
Kristen Rudd • April 18, 2024
Hi, I wanted to do some extra practice beyond the "try this"section and tried the mutate function to shorten the text labels. however, I got an error that said "could not find the "mutate" function. Am I missing something?
install.packages("scales")
library(scales)
pengiun_bill_length_by_island_v2 <- pengiun_bill_length_by_island |> mutate(mean_bill_length_one_digit = number(mean_bill_length, accuracy = 0.1))
Christopher Scanlon • November 25, 2024
Whenever I create a new variable (eg penguin_bill_length_by_island_v2) in the .r file, I get the following error: Error: object 'penguin_bill_length_by_island_v2' not found
It seems that it does not create the variable.
It's only after I run the line of code in the console that it works. In other words, I can't just write the following line of code in the .r file and have it run first time.
penguin_bill_length_by_island_v2 <- penguin_bill_length_by_island |> mutate(mean_bill_length_one_digit = number(mean_bill_length, accuracy = 0.1))
I have to run the full line of code in the console first, and then I can use it in the .r file.
I have tried this on R Studio on the desktop and in the cloud. Not sure why it doesn't work.
David Keyes Founder • November 26, 2024
Yes, you are correct that you need to run the R script file each time in order to recreate any objects.