Skip to content
R for the Rest of Us Logo

ggtext examples and exercises

This lesson is locked

Get access to all lessons in this 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.

Your Turn

Adjust the code below, using ggtext to make the word ‘plastic’ bold in the title and wrapping the subtitle so it is multi-line.

library(tidyverse)
library(ggtext)

##Your turn

#Use ggtext to make the word 'plastic' bold in the title
#and wrap the subtitle so it is multi-line

plastics <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-01-26/plastics.csv')

plastics %>%
  group_by(year) %>%
  summarize(total = sum(grand_total, na.rm = TRUE)) %>%
  ggplot() +
  geom_bar(aes(x = "", y = total, fill = as.character(year)), stat = "identity", width = 1) +
  coord_polar("y", start = 0) +
  theme_minimal() +
  labs(title = "Plastic pollution, by year", fill = "",
       subtitle = "This is a really really really long subtitle that has tons of detail about your methods and data and even more info beyond that") +
  theme(axis.title = element_blank(), panel.grid = element_blank(),
        axis.text = element_blank())

Learn More

Have any questions? Put them below and we will help you out!

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

Deepak Varughese

Deepak Varughese

January 29, 2023

Can i not use the same approach to create a multiline subtitle for a combination of plots using patchwork? It throws the following error

"Error: gridtext has encountered a tag that isn't supported yet: Only a very limited number of tags are currently supported. "

Deepak Varughese

Deepak Varughese

January 29, 2023

combined + plot_annotation(title = "Plastic pollution in India in 2019 and 2020 ", subtitle = "Data available at https://brandaudit.breakfreefromplastic.org",

                        tag_levels = c(1),
                        tag_prefix = "Fig. ") &amp;

theme(plot.tag = element_text(size = 10), plot.title = element_textbox_simple(), plot.subtitle = element_textbox_simple() )

David Keyes

David Keyes

January 30, 2023

The ggtext only uses a small set of HTML tags (e.g. italics, bold). I can't see your full code so I don't know exactly what's going on, but I'm guessing you tried to use some HTML that isn't supported (e.g. a link). Does that seem like it might be what's going on?