• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / R Tips / 10 Tips to Customize Text Color, Font, Size in ggplot2 with element_text()

10 Tips to Customize Text Color, Font, Size in ggplot2 with element_text()

May 22, 2021 by cmdlinetips

ggplot2’s theme system give us a great control over how the “non-data” elements of a plot should look like. The theme system helps elevate the plot you make by making finer changes and make it easy to look better. ggplot2’s theme system comes with multiple element_ functions,
  1. element_text()
  2. element_line()
  3. element_rect()
  4. element_blank()

And they help control how the non-data elements of a plot should like. In this tutorial we will focus on the theme element element_text(). We will learn 10 tips to have finer control over the “font size, colour and face” for many part of text elements in a plot, like title, subtitle, labels, and legend. We will use theme() function in combination with different elements associated with the plot.

Anatomy of ggplot2’s element_text()

While making a plot with ggplot2, it automatically chooses appropriate values for various aspects of element text. In total there are 10 (at least?) different aspects/elements of texts in a plot made with ggplot2. The figure below shows the anatomy of text elements and the key word in ggplot2 describing the element (Phew…My life time goal of making an “anatomy of something” plot is achieved :-)) .

With element_text(), we can customize the looks of the the texts. For example, to control the look of title of a plot, we will use “plot.title” element as argument to theme() function and use element_text() to specify the color, font and size of the plot title.

Tips to Customize Text in ggplot2 plot
Tips to Customize Text in ggplot2 plot with element_text()

Here is the list of the elements (most common?) that we can control using using element_text().

  1. axis.title.x: to customize x-axis label/title
  2. axis.title.y : to customize y-axis label/title
  3. axis.text.x : to customize x-axis tick labels
  4. axis.text.y : to customize y-axis tick labels
  5. legend.title: to customize legend title text
  6. legend.text: to customize legend text
  7. plot.title: to customize plot title
  8. plot.subtitle: to customize plot subtitle
  9. plot.caption: to customize plot’s caption
  10. plot.tag: to customize plot’s tag

Let us get started by making a scatter plot using ggplot2 with common text annotations like title, subtitle, legend and tags.

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))

We use Palmer penguins data make the scatterplot. Check out the labs() function for variation addition to the plot. We use theme_set() option to make the plot in black and white theme with pre-specified font sizes.

p<- penguins %>%
  drop_na() %>%
  ggplot(aes(x=flipper_length_mm,
             y=bill_length_mm, 
             color=species,
             shape=sex))+
  geom_point()+
  labs(title="Palmer Penguins",
       subtitle="Flipper Length vs Bill Length",
       caption="cmdlinetips.com",
       tag = 'A'
       )

p

And this is how the scatter plot looks like this. We have saved the code to make the plot as a variable and use it use it to customize 10 different text elements of ggplot2’s theme using element_text() function.

How To Customize ggplot2's text with element_text()?
How To Customize ggplot2’s text with element_text()?

Using element_text() element, we can modify multiple aspects of the text as shown below.

element_text(
  family = NULL,
  face = NULL,
  colour = NULL,
  size = NULL,
  hjust = NULL,
  vjust = NULL,
  angle = NULL,
  lineheight = NULL,
  color = NULL,
  margin = NULL,
  debug = NULL,
  inherit.blank = FALSE
)

However, in this post we will mainly focus how to customize, color of the text, size of the text, font family and font face (bold font or not).

1. Customizing ggplot2 x-axis label with axis.title.x

We can use axis.title.x element of element_text() to change the color, size and angle of the x-axis label text or title.

p + theme(axis.title.x = element_text(size=16, 
                                      color="purple", 
                                      face="bold",
                                      angle=0))
ggsave("customize_x_axis_title_with_element_text_theme_ggplot2.png")

In this example, we set the size=16, color=”purple” and bold font for x-axis label.

Customize x-axis title
Customize x-axis title

2. Customizing ggplot2 y-axis label with element_text()

We can use axis.title.y element of element_text() to change the color, size and angle of the y-axis label text or title.

p + theme(axis.title.y = element_text(size=16, 
                                      color="purple", 
                                      face="bold",
                                      angle=90))
ggsave("customize_y_axis_title_with_element_text_theme_ggplot2.png")

In this example, we set the size=16, color=”purple” angle=90 and bold font for y-axis label.

Customize y-axis title
Customize y-axis title

3. Customizing ggplot2 x-tick marks with element_text()

We can use axis.text.x element of element_text() to change the color, size and angle of the x-axis tick label text.

# x axis tick mark labels
p + theme(axis.text.x= element_text(family = "Tahoma", 
                                         face="bold", colour="black",
                                         size=10))
ggsave("customize_x_axis_tick_text_with_element_text_theme_ggplot2.png")

In this example, we set the size=10, color=”black” and bold font for x-axis tick label. In addition, we also change the Tahoma font using family argument.

Customize x-axis tick text
Customize x-axis tick text ggplot2

4. Customizing ggplot2 y-tick marks with element_text()

We can use axis.text.y element of element_text() to change the color, size and angle of the y-axis tick label text.

# y axis tick mark labels
p + theme(axis.text.y = element_text(family = "Tahoma",
                                     face="bold",
                                     colour="black",
                                     size=10))
ggsave("customize_y_axis_tick_text_with_element_text_theme_ggplot2.png")

In this example, we set the size=10, color=”black” and bold font for x-axis tick label.

Customize y-axis tick text with element_text()
Customize y-axis tick text with element_text()

5. Customizing ggplot2 legend title text with element_text()

We use legend.title element of element_text() to change the color, size and angle of legend’s title text.

p + theme(legend.title=element_text(color="purple",
                                         face="bold",size=12))
gsave("customize_legend_title_text_with_element_text_theme_ggplot2.png")

In this example, we set the size=12, color=”purple” and bold font for legend title.

Customize legend title text with element_text()
Customize legend title text with element_text()

6. Customizing ggplot2 legend text with element_text()

We use legend.text element of element_text() to change the color, size and angle of legend’s title text.

#legend.text = element_text(color, size, face)
p + theme(legend.text=element_text(face="bold", color="green",size=10))
ggsave("customize_legend_text_with_element_text_theme_ggplot2.png")

In this example, we set the size=12, color=”green” and bold font for legend text.

Customize legend text with element_text()
Customize legend text with element_text()

7. Customizing ggplot2 title text with element_text()

We use plot.title element of element_text() to change the color, and size of the plot’s title text. Here, we set the size=24, color=”blue” and bold font for plot title.

#library(extrafont)
p + theme(plot.title= element_text(size=24,
                                   color="blue",
                                   face="bold",
                                   family = "Tahoma"))
ggsave("customize_plot_title_with_element_text_theme_ggplot2.png")
Customize plot title with element_text()
Customize plot title with element_text()

8. Customizing ggplot2 subtitle text with element_text()

We use plot.subtitle element of element_text() to change the color, and size of the plot’s title text. Here, we set the size=16, color=”red” and bold font for the plot’s subtitle.

p + theme(plot.subtitle= element_text(size=16,
                                      color="red",
                                      face="bold"))
ggsave("customize_plot_subtitle_with_element_text_theme_ggplot2.png")
Customize plot subtitle with element_text()
Customize plot subtitle with element_text()

9. Customizing ggplot2 caption with element_text()

To change the plot’s caption text’s size, color and face, we can use plot.caption element.

p + theme(plot.caption= element_text(size=16,
                                   color="pink",
                                   face="bold"))
ggsave("customize_plot_caption_with_element_text_theme_ggplot2.png")
Customize plot caption with element_text()
Customize plot caption with element_text()

10. Customizing ggplot2 tag with element_text()

Similarly, to change the plot’s tag size, color and face, we can use plot.tag element with element_text() function.

p + theme(plot.tag = element_text(size=16,
                                   color="red",
                                   face="bold"))
ggsave("customize_plot_tag_with_element_text_theme_ggplot2.png")

Customize plot tag with element_text()
Customize plot tag with element_text()

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Anatomy of Rectangle Elements in ggplot27 Tips to customize rectangle elements in ggplot2 element_rect() Tips to Customize line elements in ggplot215 Tips to Customize lines in ggplot2 with element_line() remove ggplot2 elements with element_blank8 tips to use element_blank() in ggplot2 theme Scatter plot tips: Color & Shape by variable9 Tips to Make Better Scatter Plots with ggplot2 in R

Filed Under: ggplot2 element_text, ggplot2 theme, R, R Tips, tidyverse 101 Tagged With: ggplot customize element_text(), ggplot2, R

Reader Interactions

Trackbacks

  1. 15 Tips to Customize lines in ggplot2 with element_line() - Python and R Tips says:
    May 28, 2021 at 2:08 pm

    […] element_text() […]

  2. 8 tips to use element_blank() in ggplot2 theme - Python and R Tips says:
    June 13, 2021 at 8:53 am

    […] element_text() […]

  3. 7 Tips to customize rectangle elements in ggplot2 element_rect() - Python and R Tips says:
    June 13, 2021 at 8:54 am

    […] the blog post series on how to control theme elements in ggplot2, in this post we will look at how to customize […]

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version