52

In my study I am generating various graphs using R. I see that most of the graphs come up with a Sans Serif type font with various sizes.

How to I change all the text in a graph (x-label, y-label, title, legend etc.) into a uniform font e.g. Times New Roman, 12pt, Bold?

Indian
  • 977
  • 3
  • 12
  • 24

4 Answers4

52

You can use the extrafont package.

install.packages("extrafont")
library(extrafont)
font_import()
loadfonts(device="win")       #Register fonts for Windows bitmap output
fonts()                       #vector of font family names
##  [1] "Andale Mono"                  "AppleMyungjo"                
##  [3] "Arial Black"                  "Arial"                       
##  [5] "Arial Narrow"                 "Arial Rounded MT Bold"  

library(ggplot2)
data(mtcars)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +     
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme_bw() +
  theme(text=element_text(family="Times New Roman", face="bold", size=12)) #Times New Roman, 12pt, Bold
#example taken from the Github project page

enter image description here

Note: Using the extrafont package, you can also embed these fonts in PDF and EPS files (make plots in R and export to PDF/EPS). You can also directly create math symbols (see math equation in plot below), usually created using TeX. More information here and here. Also look at the github project page.

enter image description here

Also look at this answer which describes creating xkcd style graphs using the extrafont package.

enter image description here

Community
  • 1
  • 1
Ujjwal
  • 3,088
  • 4
  • 28
  • 36
23

You can change the font in Windows to Times New Roman using the windowsFonts() command and the family option in plot:

x = seq(1,10,1)
y = 1.5*x
windowsFonts(A = windowsFont("Times New Roman"))
plot(x, y,
  family="A",
  main = "title",
  font=2)

Bold text comes from font=2. As for the size, see ?cex(). Also, see here: http://www.statmethods.net/advgraphs/parameters.html

enter image description here

Devon
  • 650
  • 8
  • 19
  • This solution ONLY works for Times New Roman font. I'm trying to use this solution to change the font to something other than Times New Roman but no matter what I put in the font NEVER changes. What is a person supposed to do if they want a font other than Times New Roman? – Jennifer B. Apr 29 '21 at 16:35
  • windowsFonts(A = windowsFont("Helvetica")) plot(forecast(fit, 5), xlab ="Year", ylab ="Passengers", main ="Carnival Cruises: Passenger Volume (In Thousands)", col.main ="darkgreen", xlim = c(2005, 2025), ylim = c(7000, 16000), family="A", las="0") – Jennifer B. Apr 29 '21 at 16:36
19

Here's a ggplot solution using WindowsFonts(...)

windowsFonts(Times=windowsFont("Times New Roman"))
library(ggplot2)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme_bw() +
  theme(text=element_text(family="Times", face="bold", size=12)) #Times New Roman, 12pt, Bold

As you can see, the text really is Times New Roman.

The main idea is that whatever name you give the font internally in R, using

windowsFonts(name=windowsFont("system name"))

you should use to reference the font in

theme(text=element_text(family="name",...),...)
jlhoward
  • 58,004
  • 7
  • 97
  • 140
5

UPDATE 2020 This can now be solved by using the ggtext package, e.g.:

install.packages(ggtext)

plot <- plot + theme(
  legend.text = ggtext::element_markdown(family = 'Times', face='bold')
)

In addition, you can use markdown to complement your plot text. I experienced ggtext simpler and more fail safe than extrafont.

Comfort Eagle
  • 2,112
  • 2
  • 22
  • 44