Changing Font To Times New Roman In R Studio

NewChanging Font To Times New Roman In R Studio
  • We have 11 free Times-new-roman, Regular Fonts to offer for direct downloading 1001 Fonts is your favorite site for free fonts since 2001.
  • Once upon a time, I changed my ggplot2 font using using windowsFonts(Times=windowsFont('TT Times New Roman')) to change it. Now I can't get it off of this. In trying to set family=' in ggplot2 theme I can't seem to generate a change in fonts as I compile the MWE below with different font families.
  • In the following section, I show you 4 simple steps to follow if you want to generate a word cloud with R. STEP 1: Retrieving the data and uploading the packages To generate word clouds, you need to download the wordcloud package in R as well as the RcolorBrewer package for the colours.

Skip to instructions for macOS or Windows

When it comes to making figures in R, you can use any font you like, as long as it's Helvetica, Times, or Courier. Using other fonts that are installed on your computer can seem an impossible task, especially if you want to save the output to PDF. Fortunately, the extrafont package makes this process much easier. With it, you can create.

R and ggplot can create fantastic graphs, but the default Arial/Helvetica font is too boring and standard. You can change the font used in a plot fairly easily three different ways:

  1. All of the built-in ggplot themes have a base_family argument for setting the overall font family for the plot
  2. element_text() has a family argument for changing fonts on individual plot elements
  3. geom_text() and annotate(geom = 'text', ...) have a family argument for changing fonts on text layers

For example:

However, there are a couple difficulties when using custom fonts like this:

  1. R on Windows does not automatically see custom fonts and will throw an error if you try to use them.
  2. ggsave() on its own cannot correctly save PDF versions of plots with custom fonts—it cannot embed the fonts.

Fixing both of these issues is relatively easy. On Windows, you can either load fonts into R on the fly with windowsFonts(name_of_font_inside_r = windowsFont('Name of actual font')), or you can use extrafonts::load_fonts() from the extrafonts library to permanently load fonts into R’s internal database. A full example of this is included below.

Embedding fonts in PDFs is also fairly easy. Instead of using R’s default PDF-writing engine, you can use the Cairo graphics library (which, nowadays, is conveniently packaged with R). Cairo has full Unicode support and can handle embedding custom fonts just fine. To make ggsave() use the Cairo engine when writing a PDF, specify the device:

You can also use Cairo’s PNG engine when writing PNG files. R’s default PNG-writing engine can sometimes have issues with correctly setting the resolution. In theory, if you specify a width and a height and a DPI, ggsave() will generate a file with those dimensions. However, if you place the PNG into Word, PowerPoint, InDesign, or any other programs, the graphic will be too large, for reasons unknown. If you save the graphic with the Cairo library, though, these programs will respect the size and DPI and place the image correctly.

Using the Cairo PNG library makes a significant difference when you use the image in other programs. Notice how the Cairo-based PNG is actually 4 inches wide in Word, while R’s default PNG takes up the full width of the page and uses a lower resolution:

Changing Font To Times New Roman In R Studio 8

Finally, if you use R Markdown and knitr, you can specify the Cairo device for each output type in the document metadata:

Here’s how you can use ggplot::ggsave() and Cairo to create PDF with embedded custom fonts and PNGs with correct resolutions:

Full instructions for macOS

Changing Font To Times New Roman In R Studio 3

The Cairo graphics library should be installed behind the scenes when you install R—you should not need to install any R-specific Cairo libraries or anything for this to work. However, you do need to install an X11 window system first, like XQuartz.

Changing font to times new roman in r studio 8

Changing Font To Times New Roman In R Studio Free

You can verify that you have Cairo support by running the capabilities() function; TRUE should show up under cairo:

R on macOS should automatically see the fonts you have installed on your computer.

Change Font To Times New Roman

Here’s a full example of loading and using a custom font on macOS:

Full instructions for Windows

The Cairo graphics library should be installed behind the scenes when you install R—you should not need to install any special Cairo libraries or anything for this to work.

You can verify that you have Cairo support by running the capabilities() function; TRUE should show up under cairo:

R on Windows cannot see the fonts you have installed on your computer. You can see a list of fonts R does have access to with the windowsFonts() function:

You can add all your system fonts to that database by installing the extrafont library and running font_import(). This will take a while, though, and it will only pick up fonts that are currently installed. If you install a font later, R will not see it—you’ll need to run extrafont::font_import() again.

Changing Font To Times New Roman In R Studio

Alternatively, you can load fonts into R on the fly, without loading the full database, using windowsFonts(name_of_font_inside_r = windowsFont('Name of actual font')):

Changing Font To Times New Roman In R Studio 3.5

Once you do this, the font will be loaded:

Changing Font To Times New Roman In R Studio Software

This only takes effect for your current R session, so if you are knitting a document or if you ever plan on closing RStudio, you’ll need to incorporate this font assignment code into your script. If you don’t want to do that, run extrafont::load_fonts() to load all the fonts—once you do this, you won’t need to repeatedly run windowsFonts() to load fonts each time you run a script.

Here’s a full example of loading and using a custom font on Windows: