Computer Modern fonts in Lattice plots in Sweave
documents
This document describes (and demonstrates) the steps that are necessary to
produce an Sweave document that includes code chunks that create lattice plots
that use Computer Modern fonts.
A complete understanding can be gained by reading the processed PDF file
cmTutorial.pdf and the cmTutorial.Rnw “source” file.
1. Download a set of Computer Modern fonts with non-TEX encodings. This
document uses the cm-lgc LATEX package by Alexej Kryukov.1
This tutorial assumes that the cm-lgc directory (and its sub-directories)
have been placed in the same directory as the main Sweave document. It
is more efficient and proper to install them somewhere central, but such
issues are beyond the scope of this tutorial.
2. If you are going to use R’s “mathematical annotation” features (see ?plot-
math), then you should also download the special cmsyase font.2
It is assumed that the files cmsyase.afm and cmsyase.pfb are stored in
the same directory as the main Sweave document.
3. Within the Sweave document, the Computer Modern fonts need to be
registered with the PDF and PostScript font databases.
> CM <- Type1Font("CM", c(file.path("cm-lgc", "fonts",
+
"afm", "public", "cm-lgc", c("fcmr8a.afm",
+
"fcmb8a.afm", "fcmri8a.afm", "fcmbi8a.afm")),
+
"./cmsyase.afm"))
> pdfFonts(CM = CM)
> postscriptFonts(CM = CM)
4. Set the Computer Modern fonts as the default font family.
> ps.options(family = "CM")
5. Draw a pretty picture.3
> histogram(~height | voice.part, data = singer,
+
nint = 17, endpoints = c(59.5, 76.5), layout = c(2,
+
4), aspect = 1, xlab = "Height (inches)")
1http://www.ctan.org/tex-archive/help/Catalogue/entries/cm-lgc.html
2http://www.stat.auckland.ac.nz/~paul/R/CM/CMR.html
3Because this is a lattice plot, I use one chunk to show the code and another chunk, which
reuses the first chunk, to produce the plot. See the “source” file cmTutorial.Rnw.
1
Height (inches)
Percent of Total0
10
20
30
40
60
65
70
75
Bass 2
Bass 1
Tenor 2
0
10
20
30
40
Tenor 1
0
10
20
30
40
Alto 2
A