Setting fonts for annotations and titles

For most data analysis, we can just use the default fonts for titles. However, sometimes, we might want to choose different fonts for presentation and publication purposes. Selecting fonts can be tricky, as this depends on the operating system and the graphics device. We will see some simple ways to choose fonts in this recipe.

Getting ready

All you need to try out in this recipe is to run R and type the recipe in the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.

How to do it...

The font family and face can be set with the par() command:

par(family="serif",font=2)

How it works...

A font is specified in two parts: a font family (such as Helvetica or Arial) and a font face within family (such as bold or italic).

The available font families vary by operating system and graphics devices. So, R provides some proxy values that are mapped on to the relevant available fonts irrespective of the system. Standard values for family are serif, sans, and mono.

The font argument takes numerical values: 1 corresponds to plain text (the default), 2 to bold face, 3 to italic, and 4 to bold italic.

For example, par(family="serif",font=2) sets the font to a bold Times New Roman font on Windows. You can check the other font mappings by running the windowsFonts() command at the R prompt.

The fonts for axis annotations, labels, and plot main title can be set separately using the font.axis, font.lab, and font.main arguments, respectively.

There's more...

The choice of fonts is very limited if we just use the proxy family names. However, we can use a wide range of fonts if we are exporting our graphs in the PostScript or PDF formats. The postscriptFonts() and pdfFonts() functions show us all the available fonts for these devices. To see the PDF fonts, run the following command:

names(pdfFonts())
 [1] "serif"              "sans"               "mono"                
 [4] "AvantGarde"        "Bookman"             "Courier"             
 [7] "Helvetica"         "Helvetica-Narrow"    "NewCenturySchoolbook"
[10] "Palatino"          "Times"               "URWGothic"           
[13] "URWBookman"        "NimbusMon"           "NimbusSan"           
[16] "URWHelvetica"      "NimbusSanCond"       "CenturySch"          
[19] "URWPalladio"       "NimbusRom"           "URWTimes"            
[22] "Japan1"            "Japan1HeiMin"        "Japan1GothicBBB"     
[25] "Japan1Ryumin"      "Korea1"              "Korea1deb"           
[28] "CNS1"              "GB1"                 

To use one of these font families in a PDF, we can pass the family argument to the pdf() function:

pdf(family="AvantGarde") pdf(paste(family="AvantGarde")

See also

In Chapter 15, Finalizing Graphs for Publications and Presentations, we will see some more practical recipes on setting fonts for publications and presentations.

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

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