Skip to content
R for the Rest of Us Logo

Text and Labels

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.

Your Turn

Complete the text and labels sections of the data-visualization-exercises.Rmd file.

Learn More

text and labels Resources

Data Visualization: A Practical Introduction has a section in Chapter 5 on adding text to plots, as does Chapter 28 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!

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

Catherine Roller White

Catherine Roller White

April 10, 2021

When I rounded the value labels for average number of hours of sleep, they displayed as 7 and 6.9. I like to keep the number of decimals displayed consistent, even if the decimal is equal to zero. How can I round to one decimal but have 7.0 display as 7.0 instead of 7? Thank you.

Megan Ruxton

Megan Ruxton

April 14, 2021

If I have a bar chart with a frequency on the y axis, how do I apply the geom_text and geom_label? For example, in my project I begin with the following code: ggplot(data = intro_etc, aes(x = gender_identity, fill = gender_identity))+ geom_bar() So there isn't anything listed for the y axis for me to enter into the command for geom_text or geom_label.

Lisa Janz

Lisa Janz

May 11, 2021

The colour of the text keeps turning up red rather than white. I have tried with other colours, but none of them work. I also tried ordering the code the way that it was done in the solutions. Any suggestions as to why this is happening? Is there a library that I haven't loaded? I will also note that in the legend there is a spot for "colour", so it looks like the program has coded colour as a variable. ggplot(sleep_by_gender, mapping = aes(x = gender, y = avg_sleep, fill = gender)) + geom_col() + scale_fill_manual(values = c("yellow", "orange"))+scale_y_continuous(breaks = c(seq(from = 0, to = 8, by = 1))) + geom_text (aes(label = round(avg_sleep, 1), vjust = 1.5, colour = "white"))

Lisa Janz

Lisa Janz

May 11, 2021

To position the labels in this exercise, you use vjust = -1.1

Negative and positive values were not discussed in the video and if I use -1.1, the label disappears from the graph. Can you clarify? Is this just a mistake in the tutorial or is my graph too small. What do the numbers represent? Are they related to the width of bins?

Will there be any tutorials on how to use Help files? I've yet to successfully use one. I entered ?geom_text into the console and got to the help file but have no idea where I'm supposed to put the relevant code and not confident that I know how to parse what it's telling me. For this specific task, I added :show.legend = FALSE: to the very end of my code, but that just removed an "a" within the label. How was I supposed to know from only reading the Help file that I needed put it in 2 places?

sleep_by_gender% mutate(avg_sleep = round(avg_sleep, 1)) ggplot(data=sleep_by_gender, mapping=aes(x=gender, y=avg_sleep, fill=gender))+ geom_col()+ scale_color_brewer(type = "div", palette = "PuOr")+ scale_y_continuous(limits = c(0, 8), breaks = c(0, 1, 2, 4, 6, 8))+ geom_label(aes(label=avg_sleep), vjust= -.5, color= "white", show.legend = FALSE)

Jay Cutler

Jay Cutler

March 4, 2022

Hi David, with your code here...

geom_text(aes(label = round(avg_sleep, 1)), vjust = 1.5, color = "white")

My intuitive way of typing the code was to include the 'vjust' and 'color' arguments within the aesthetics parenthetical, like so:

geom_text(aes(label = round(avg_sleep, 1), vjust = 1.5, color = "white"))

...which largely worked, but my text was a strange pink color instead of white. I'm curious why this is the case? Any rhyme or reason to distinguish between what arguments end up with the 'label' piece in the aesthetics parenthetical and what arguments live freely in the geom_text function? Thanks!

Zaynaib Giwa

Zaynaib Giwa

April 1, 2022

Hello everyone, For some reason, I can't get the text to turn white. Hopefully, I can get a second pair of eyes on my code. It changes the color to the fill color of female.

ggplot(sleep_by_gender, aes(x=gender, y=avg_sleep, fill=gender)) + geom_col() + scale_y_continuous(limits=c(0,8), breaks = c(0,1,2,3,4,5,6,7,8)) + geom_text(aes(label = round(avg_sleep,1), vjust = 1, color = "white"

            )
        )

Jordan Helms

Jordan Helms

April 7, 2022

Why do we have to turn the legend off in two places?

Why have you added the following code line: na.value = "blue"

Niger Sultana

Niger Sultana

May 25, 2022

Hi I wanted to add superscript, subscript as label on x axis through ggtext package? for example, WD (mg mm-3), -3 will be superscript in R script and Markdown file? or will this work : geom_text(aes(label = paste(WD, (mg)"^(",-3 , ")", sep = "")), parse = TRUE)

Thanks in advance.

Kirstin O'Dell

Kirstin O'Dell

October 19, 2022

To turn the legend off I used theme (legend.position = "none") and it worked. I wondered if there was a difference between using that code and the way you showed it?