How to selectively add color to title text in ggplot2

In this tutorial, we will learn how to specify colors to selective words of title text in ggplot2. For example, if we had made a plot with ggplot2 and have title text to describe the plot, we will learn how to color certain words of the title not the whole title text. We will use the R package ggtext (from Wilke’s lab) to add color to the words in the title text.

Let us load the packages needed. We are using the R package tidyquant to get stock market data from R and will use the data to make grouped barplot of stock returns.

library(ggtext)
library(tidyquant)
library(tidyverse)
theme_set(theme_bw(16))

Let us download every day stock price data since January 2021 for the tech giant Google/Alphabet and the US market index S&P 500. We use tidyquant’s tq_get() function to download the daily stock price data for these two tickers.

stock_tickers = c("SPY","NVDA")
stock_df <- stock_tickers |>
  tq_get(from = "2021-01-01", to=Sys.Date()) 
stock_df %>% head()

# A tibble: 6 × 8
  symbol date        open  high   low close    volume adjusted
  <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
1 SPY    2021-01-04  375.  375.  365.  369. 110210800     351.
2 SPY    2021-01-05  368.  372.  368.  371.  66426200     353.
3 SPY    2021-01-06  370.  377.  369.  374. 107997700     355.
4 SPY    2021-01-07  376.  380.  376.  379.  68766800     360.
5 SPY    2021-01-08  381.  381.  377.  381.  71677200     362.
6 SPY    2021-01-11  378.  381.  378.  379.  51034700     360.

We use the daily stock data to compute yearly returns using tq_transmute() function in tidyquant.

yearly_return <- stock_df |>
  group_by(symbol) |>
  tq_transmute(select     = adjusted,
               mutate_fun = periodReturn,
               period     = "yearly") |>
  ungroup()
yearly_return %>% head()

# A tibble: 6 × 3
  symbol date       yearly.returns
  <chr>  <date>              <dbl>
1 SPY    2021-12-31          0.305
2 SPY    2022-12-30         -0.182
3 SPY    2023-12-29          0.262
4 SPY    2024-07-05          0.174
5 NVDA   2021-12-31          1.24 
6 NVDA   2022-12-30         -0.503

Now we have the data ready to make grouped barplot of the yearly returns. We also have small title text describing the plot. Let us add colors to part of the title text in the plot.

yearly_return %>%
  ggplot(aes(x=year(date), y=yearly.returns, fill=symbol))+
  geom_col(position="dodge")+
  scale_y_continuous(breaks=scales::breaks_pretty(6),
                     labels = scales::percent_format())+
  scale_fill_brewer(palette="Dark2")+
  labs(x=NULL,
       title="Yearly Returns: S&P 500 vs Nvidia ")
ggsave("How_to_selectively_add_color_to_title_text_ggplot2.png")
How to selectively add color to title text in ggplot2
How to selectively add color to title text in ggplot2

Color selective words in ggplot2 title with ggtext

To add colors to select words in the title, we use the R package ggtext to specify colors to title text. To do that we need to tweak the code in two places. First we sopecify the color we want around the words of interest using the tag that looks like this ““. Then we add plot.title = element_markdown() to the theme function.

yearly_return %>%
  ggplot(aes(x=year(date), y=yearly.returns, fill=symbol))+
  geom_col(position="dodge")+
  scale_y_continuous(breaks=scales::breaks_pretty(6),
                     labels = scales::percent_format())+
  scale_fill_brewer(palette="Dark2")+
  labs(x=NULL,
       title="Yearly Returns: <span style='color:steelblue'>Nvidia vs S&P 500</span>") +
  theme(plot.title = element_markdown())
ggsave("How_to_selectively_add_color_to_title_text_with_ggtext_ggplot2.png")

Then we get the plot with selectively added colors to words in the title text.

Add colors to select words in title text with ggtext

You might want to check other relevant posts on using ggtext to add colors to your plot made with ggplot2.

Exit mobile version