Choosing font families and styles under Windows, Mac OS X, and Linux

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.

Getting ready

We are only using base graphics functions for this recipe. So, just open up the R prompt and type the following code. You may wish to save the code as an R script for later use.

How to do it...

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 
(family="sans", font=1)", 
family="sans",font=1,adj=0)
text(0,140,"Arial Bold 
(family="sans", font=2)", 
family="sans",font=2,adj=0)
text(0,100,"Arial Italic 
(family="sans", font=3)", 
family="sans",font=3,adj=0)
text(0,60,"Arial Bold Italic 
(family="sans", font=4)", 
family="sans",font=4,adj=0)

text(70,180,"Times 
(family="serif", font=1)", 
family="serif",font=1,adj=0)
text(70,140,"Times Bold 
(family="serif", font=2)", 
family="serif",font=2,adj=0)
text(70,100,"Times Italic 
(family="serif", font=3)", 
family="serif",font=3,adj=0)
text(70,60,"Times Bold Italic 
(family="serif", font=4)", 
family="serif",font=4,adj=0)

text(130,180,"Courier New
(family="mono", font=1)",
family="mono",font=1,adj=0)
text(130,140,"Courier New Bold 
(family="mono", font=2)", 
family="mono",font=2,adj=0)
text(130,100,"Courier New Italic 
(family="mono", font=3)",
family="mono",font=3,adj=0)
text(130,60,"Courier New Bold Italic 
(family="mono", 
font=4)", 
family="mono",font=4,adj=0)
How to do it...

How it works...

In the 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 the example, we created a plot area with X and Y co-ordinates 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 12 combinations of the three font families and four font faces.

There's more...

As you may 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 font Georgia 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 like 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.

See also

In the next recipe we will see how to use additional font families available for vector formats such as PDF and PS.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset