In this recipe, we will see how to choose font families and styles under the three most popular operating systems, namely, Windows, Mac OS X, and Linux.
We will only use the base graphics functions for this recipe. So, just open up the R prompt and type in the following code. You might wish to save the code as an R script for later use.
Let's look at all the basic default fonts available under Windows:
par(mar=c(1,1,5,1)) plot(1:200,type="n",main="Fonts under Windows",axes=FALSE,xlab="",ylab="") text(0,180,"Arial \n(family=\"sans\", font=1)", family="sans",font=1,adj=0) text(0,140,"Arial Bold \n(family=\"sans\", font=2)", family="sans",font=2,adj=0) text(0,100,"Arial Italic \n(family=\"sans\", font=3)", family="sans",font=3,adj=0) text(0,60,"Arial Bold Italic \n(family=\"sans\", font=4)", family="sans",font=4,adj=0) text(70,180,"Times \n(family=\"serif\", font=1)", family="serif",font=1,adj=0) text(70,140,"Times Bold \n(family=\"serif\", font=2)", family="serif",font=2,adj=0) text(70,100,"Times Italic \n(family=\"serif\", font=3)", family="serif",font=3,adj=0) text(70,60,"Times Bold Italic \n(family=\"serif\", font=4)", family="serif",font=4,adj=0) text(130,180,"Courier New\n(family=\"mono\", font=1)", family="mono",font=1,adj=0) text(130,140,"Courier New Bold \n(family=\"mono\", font=2)", family="mono",font=2,adj=0) text(130,100,"Courier New Italic \n(family=\"mono\", font=3)", family="mono",font=3,adj=0) text(130,60,"Courier New Bold Italic \n(family=\"mono\", font=4)", family="mono",font=4,adj=0)
In this example, we demonstrated all the combinations of the basic font faces and families available in R under Windows. Fonts are specified in R by choosing a font family and a font face. There are three main font families: sans, serif, and mono, which are mapped on to specific fonts under different operating systems. As shown in the example, under Windows, sans maps to Arial, serif to Times New Roman, and mono to Courier New. The font family is specified by the family
argument, which can be passed to the text()
function (as in the example) or in par()
(thus applied to all text in the plot), mtext()
, and title()
.
The font face can take four basic values denoted by the numbers 1
to 4
, which stand for regular, bold, italic, and bold italic, respectively. The default value of font is 1
. Note that font only applies to text inside the plot area. To set the font face for axis annotations, labels and the plot title, we need to use font.axis
, font.lab
, and font.main
, respectively.
In this example, we created a plot area with the x and y coordinates running from 0
to 200
each, but suppressed drawing of any axes or annotations. Then, we used the text()
function to draw text labels showing the twelve combinations of the three font families and four font faces.
As you might have noticed, we did not specify the names of the font families in the text()
command. Instead, we used the keywords, sans
, serif
, and mono
, to refer to the corresponding default fonts under Windows. We can check these font family mappings by running the windowsFonts()
command at the R prompt, which lists the names of the fonts for each of the font families. We can also add new mappings using this function. For example, to add the Georgia font, we need to run:
windowsFonts(GE = windowsFont("Georgia"))
Then, we can just set family to "GE"
to use the Georgia font:
text(150,80,"Georgia",family="GE")
Just as under Windows, there are default font families under Mac OS X and Linux. The serif and mono fonts are the same as in Windows. However, the sans font is usually Helvetica. To check the default font mappings and add new font families, we need to use the X11Fonts()
and quartzFonts()
functions under Linux and OS X, respectively.