Tag Archives: fonts

Changing the fonts in R plots

I use R to do almost all of my statistical analysis and plotting. It has a very steep learning curve but once you get into it, it’s very powerful and you can customise almost anything. The only problem is that these changes are not always obvious or easy to do. Case in point: how to change the fonts on an R plot.

Here are some R commands to plot a few made-up data points:

x < - 1:10
y <- x + rnorm(10)
plot(x,y,main="Basic R plot")

And here's the resulting plot:

Basic R plot

But say you don't want to use Arial as the font for the plot? What if you're doing a Wild West plot (I don't know why but just suppose...) and want to use something like Playbill? This can be done in the following steps.

  1. Find the font settings file Rdevga. There are two versions. The first is system-wide and can be found in $R_HOME$/etc/Rdevga (e.g. C:/Program Files/R/R-2.6.0/etc/Rdevga). The second applies only to your user account and can be found in $R_USER (find this in R using Sys.getenv("R_USER")). If Rdevga doesn't exist in your user directory, just copy the system-wide version there.
  2. Modify the user or system-wide Rdevga to add your desired font. The comments in the file explain how to do this; for example, to add Playbill, add a line that reads "TT Playbill : plain". A full list of available fonts on your system can be found (on a Windows machine) by going to Control Panel > Fonts.
  3. Count the number of uncommented non-blank lines in the file and note the number of your newly added line. After a default R installation, a line added to the bottom would be 20.
  4. (Re)start R so that it reads in the new settings. Redo your plot using the font options to select your desired font, e.g. plot(x,y,main="Basic R plot", font.main=20, cex.main=3)
  5. Et voilĂ ! New plot:
    Basic R plot with different font

Some final notes. First, you might have to change the scale of the font for it to look right (e.g. the cex.main command). Second, you can change the font for specific parts of the plot but it’s tricky to change them all at once; see the help files for more information (type ?par in the Rconsole). Third, you can have a maximum of 32 fonts defined in your Rdevga file (i.e. the standard 19 plus 13 of your choosing, unless you replace the standard ones). Finally, this change only works for functions which call the windows method; so exporting to jpg, bmp, png etc should be fine but pdf and postscript will revert to the default fonts. You can change the fonts for these plots but that’s for another time.

Hope this helps!

The importance of good typography

Adobe Centaur specimen

As a researcher, I spend a lot of time writing. Papers, reports, briefings, minutes, blogs – they all need to be done sooner or later and each format has its own challenges. There are some common features though and most importantly, each document must be tailored to the needs of the anticipated audience. This does not only mean adjusting the technical level of the content (e.g. a discussion of statistical tests isn’t very useful for a magazine article aimed at high-school students); the presentation of the document must be appropriate as well.

A well-designed document should draw the reader into the text, or at the very least, not put them off reading. Writers however often do not have the time, interest or expertise to properly typeset their own documents and as a result, texts tend to range from the abysmal to the mundane; beautiful readable documents are few and far between. While this criticism typically applies to the daily in-house documents of any office, it is equally relevant to professional academic publishers. I recently purchased a book from a major academic press which, although not poorly designed per se, is a rather uninspiring, 250 pages of boiler-plate Times New Roman. The spacing, headings, punctuation and so on are technically correct but it makes me think that I’m reading a well-formatted draft, rather than a finished product.

Typography should be an extension of the personalities of both author and text. So, just as this personal expression can be squashed by rigidly defining a style, a lack of interest in document presentation similarly deprives the author from an opportunity to present his or her content’s best possible face to the reader. In future, I plan to write more about typography here, especially for academic content in print and on the web. For homework though, try taking one of your favourite books off the shelf and look at the copyright page. You might see something like “Set in Adobe Caslon 10/12″ – typography at work!