Change Font of Plot in R (3 Examples) | Base R, ggplot2 & PDF Export (2024)

In this tutorial, I’ll illustrate how to select fonts of plots manually in R.

The article will contain three examples for the modification of font families in plots. More precisely, the content looks as follows:

1) Creation of Example Data

2) Example 1: Changing Font of Base R Plot

3) Example 2: Changing Font of ggplot2 Plot

4) Example 3: Changing Font when Exporting Plot Using pdf() Function

You’re here for the answer, so let’s get straight to the examples.

Creation of Example Data

As a first step, we need to construct some example data:

data <- data.frame(x = 1:10, # Create example data y = 1:10)data # Print example data# x y# 1 1 1# 2 2 2# 3 3 3# 4 4 4# 5 5 5# 6 6 6# 7 7 7# 8 8 8# 9 9 9# 10 10 10

As you can see based on the previous output of the RStudio console, our example data has ten rows and two columns. Both variables of our data frame contain a numeric range from 1 to 10.

Example 1: Changing Font of Base R Plot

In Example 1, I’ll explain how to change the font family of a Base R graph.

Let’s first create a plot of our data with the default font specifications:

Change Font of Plot in R (3 Examples) | Base R, ggplot2 & PDF Export (1)

As shown in Figure 1, we created a default scatterplot with the previous R syntax.

If we want to change the fonts of all elements of our plot, we can use the windowsFonts function as shown below:

windowsFonts(A = windowsFont("Times New Roman")) # Specify fontplot(data$x, # Plot with manually specified font data$y, family = "A")

Change Font of Plot in R (3 Examples) | Base R, ggplot2 & PDF Export (2)

As visualized in Figure 2, the previously shown R programming syntax created a Base R graph with the font Times New Roman.

Example 2: Changing Font of ggplot2 Plot

In Example 2, I’ll show how to adjust the text font in a ggplot2 plot.

If we want to use the functions of the ggplot2 package, we first need to install and load ggplot2:

install.packages("ggplot2") # Install & load ggplot2 packagelibrary("ggplot2")

Now, we can draw a ggplot2 scatterplot with default text specifications as shown below:

ggp <- ggplot(data, aes(x, y)) + # ggplot2 plot with default font geom_point()ggp # Draw ggplot2 plot

Change Font of Plot in R (3 Examples) | Base R, ggplot2 & PDF Export (3)

As revealed in Figure 3, the previous R code created a ggplot2 scatterplot with the typical font style.

As in Example 1, we can now use the windowsFonts command to change the font of our ggplot2 plot:

windowsFonts(A = windowsFont("Times New Roman")) # Specify fontggp + # ggplot2 plot with manually specified font theme(text = element_text(family = "A"))

Change Font of Plot in R (3 Examples) | Base R, ggplot2 & PDF Export (4)

The output of the previous R code is shown in Figure 4: A ggplot2 graph with user-defined font.

Example 3: Changing Font when Exporting Plot Using pdf() Function

In case you want to export your plot to an external file (such as PDF, JPEG, JPG etc.), you may modify the font family during the exportation process.

In this example, I’ll illustrate how to write a PDF file with manually specified font.

Have a look at the following R syntax:

pdf("ggp.pdf", family = "Times") # Apply pdf functionggpdev.off()

After running the previous R code, you should find a PDF file in your working directory that looks exactly as Figure 4 (i.e. a ggplot2 scatterplot with Times New Roman font).

Video & Further Resources

If you need more info on the R programming syntax of this article, you may want to have a look at the following video of my YouTube channel. I’m explaining the content of this tutorial in the video:

Note that we have created only scatterplots in the examples of this tutorial. However, the same logic could be applied to other types of plots such as boxplots, barcharts, density plots, line plots, histograms, and so on.

Furthermore, it would also be possible to use other font types such as Calibri, Helvetica, Futura, Garamond, or Arial.

However, in addition to the video you may have a look at the related tutorials of this website:

  • Change Font Size of ggplot2 Plot in R
  • Change Background Color of ggplot2 Plot in R
  • Change Formatting of Numbers of ggplot2 Plot Axis
  • Change Labels of ggplot2 Facet Plot in R
  • Graphics in R
  • R Programming Tutorials

This tutorial showed how to adjust the font family in Base R and ggplot2 graphics in R programming. Let me know in the comments section, if you have additional questions.

4 Comments. Leave new

  • Change Font of Plot in R (3 Examples) | Base R, ggplot2 & PDF Export (5)

    margo

    April 24, 2021 9:53 pm

    Your site is so helpful, I use it a lot. Thank you for your hard work! I finally removed annoying back ground and lines from plot generated by ggplot. Thank you very much.

    Reply
    • Change Font of Plot in R (3 Examples) | Base R, ggplot2 & PDF Export (6)

      Joachim

      April 26, 2021 6:18 am

      Hi Margo,

      Thanks a lot for the great feedback, glad to hear that it helped! 🙂

      Regards

      Joachim

      Reply
  • Change Font of Plot in R (3 Examples) | Base R, ggplot2 & PDF Export (7)

    Jenn

    August 2, 2023 4:00 pm

    Thank you for the tutorials!

    THis is helpful, but I’m still having issues… I have used the exact code above (for Base) and, while I get no errors in the code, the font on the figure output does not change. I’m using RStudio, if that helps.

    Reply
    • Change Font of Plot in R (3 Examples) | Base R, ggplot2 & PDF Export (8)

      Cansu (Statistics Globe)

      August 8, 2023 9:27 am

      Hello Jenn,

      If you haven’t fixed it yet. Could you please share your code?

      Best,
      Cansu

      Reply

Leave a Reply

I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.

Statistics Globe Newsletter

Related Tutorials

How to Use the ggplotly Function in R (2 Examples)

Draw Multiple Function Curves to Same Plot in R (2 Examples)

Change Font of Plot in R (3 Examples) | Base R, ggplot2 & PDF Export (2024)

FAQs

How to change font on R plot? ›

In this approach to change the font of the given plot, the user needs to call the windowsFont() which is one of the in-build function of the R programming language, with the name of the font as its parameter, this function is used to specify the font family as per requirement and with this, the user also need to use ...

Can I change font in Ggplot? ›

The base font size is 11 pts by default. You can change it with the base_size argument in the theme you're using. See the complete theme documentation for more high level options you can set.

What font is used in RStudio? ›

RStudio uses the best font on any system. For example, on Ubuntu, it uses Ubuntu Mono by default. On Windows, it should use Consolas by default, and Lucida Console as a fallback. Consolas is optimized for programming, whereas Courier New is a holdover from the 1950s.

How to change plot title font in ggplot? ›

Change the font appearance (text size, color and face) of titles and caption. For example, to set a bold ggplot title, use this: p + theme(plot. title = element_text(face = "bold")) . The allowed values for the font face include: “plain”, “italic”, “bold” and “bold.

How to change font style in RStudio? ›

Customizing Fonts and Appearance
  1. Smaller Text. If you need smaller text for certain paragraphs, you can enclose text in the <small> tag. ...
  2. Specifying a Stylesheet. You can use CSS to customize the display of individual slides or spans of text. ...
  3. Applying Styles. ...
  4. Overriding Default Element Styles. ...
  5. Custom Slide Types.
Jun 16, 2023

What font is used in R plots? ›

The default font families are 'sans', 'serif' and 'mono'. The first element of the vector is the normal face, then bold face, then italic face, then bold-italic font face. The bold face is used for the title of the plots, for example. plot(rnorm(100), rnorm(100), main = “A plot.”)

How do I change the axis label font size in R plot? ›

The cex. lab attribute can be used to modify the font size of the labels of the both (x and y) axes.

Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 5496

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.