Fonts

Problem

You want to use different fonts in your graphs.

Solution

Update: Also see the extrafont package for much better support of fonts for PDF and Windows bitmap output.

Font support in R is generally not very good. It varies between systems, and between output formats.

geom_text

With geom_text or annotate in ggplot2, you can set a number of properties of the text. geom_text is used to add text from the data frame, and annotate is used to add a single text element.

Name Default value
size 5
family "" (sans)
fontface plain
lineheight 1.2
angle 0
hjust 0.5
vjust 0.5

Note that size is in mm, not points.

  1. dat <- data.frame(
  2. y = 1:3,
  3. text = c("This is text", "Text with\nmultiple lines", "Some more text")
  4. )
  5. library(ggplot2)
  6. p <- ggplot(dat, aes(x=1, y=y)) +
  7. scale_y_continuous(limits=c(0.5, 3.5), breaks=NULL) +
  8. scale_x_continuous(breaks=NULL)
  9. p + geom_text(aes(label=text))
  10. p + geom_text(aes(label=text), family="Times", fontface="italic", lineheight=.8) +
  11. annotate(geom="text", x=1, y=1.5, label="Annotation text", colour="red",
  12. size=7, family="Courier", fontface="bold", angle=30)

plot of chunk unnamed-chunk-2plot of chunk unnamed-chunk-2

themes and element_text

When controlling elements such as the title, legend, axis labels, and so on, you use element_text, which has the same parameters, except that size is points (not mm), and instead of fontface, it uses face. The default value of size depends on the element; for example, titles are larger than tick labels.

  1. p + geom_point() +
  2. ggtitle("This is a Title") +
  3. theme(plot.title=element_text(family="Times", face="bold", size=20))

plot of chunk unnamed-chunk-3

Table of fonts

You can use this code to generate a graphical table of fonts. Fonts have short names and canonical family names. You can use either one when specifying the family.

  1. fonttable <- read.table(header=TRUE, sep=",", stringsAsFactors=FALSE,
  2. text='
  3. Short,Canonical
  4. mono,Courier
  5. sans,Helvetica
  6. serif,Times
  7. ,AvantGarde
  8. ,Bookman
  9. ,Helvetica-Narrow
  10. ,NewCenturySchoolbook
  11. ,Palatino
  12. ,URWGothic
  13. ,URWBookman
  14. ,NimbusMon
  15. URWHelvetica,NimbusSan
  16. ,NimbusSanCond
  17. ,CenturySch
  18. ,URWPalladio
  19. URWTimes,NimbusRom
  20. ')
  21. fonttable$pos <- 1:nrow(fonttable)
  22. library(reshape2)
  23. fonttable <- melt(fonttable, id.vars="pos", measure.vars=c("Short","Canonical"),
  24. variable.name="NameType", value.name="Font")
  25. # Make a table of faces. Make sure factors are ordered correctly
  26. facetable <- data.frame(Face = factor(c("plain","bold","italic","bold.italic"),
  27. levels = c("plain","bold","italic","bold.italic")))
  28. fullfonts <- merge(fonttable, facetable)
  29. library(ggplot2)
  30. pf <- ggplot(fullfonts, aes(x=NameType, y=pos)) +
  31. geom_text(aes(label=Font, family=Font, fontface=Face)) +
  32. facet_wrap(~ Face, ncol=2)

To view it on screen:

  1. pf

What you see on the screen isn’t necessarily the same as what you get when you output to PNG or PDF. To see PNG output:

  1. png('fonttable.png', width=720, height=720, res=72)
  2. print(pf)
  3. dev.off()

Notice that on the system that generated this picture, most of the fonts (at the top) don’t really work. Only the basic fonts (at the bottom) work.

plot of chunk unnamed-chunk-7


And PDF output (the image below has been converted from PDF to PNG):

  1. pdf('fonttable.pdf', width=10, height=10)
  2. print(pf)
  3. dev.off()
  4. # Convert to PNG using GraphicsMagick:
  5. # system("gm convert -resize 720x720 -background white fonttable.pdf fonttable-pdf.png")

The PDF device has better font support than the PNG device. All the fonts work (though they’re not necessarily pretty):

PDF font table, converted to PNG