ComputerModern font in ggplot2 graphics?

1,313 views
Skip to first unread message

James Howison

unread,
Jan 19, 2009, 5:20:49 PM1/19/09
to ggp...@googlegroups.com
I'm tidying up my dissertation and I'd like to use the same font in
ggplot2 created graphics as I'm using in LaTeX, Computer Modern.

I think I've found the appropriate function to alter the theme in the
"Polishing" chapter, ie theme_text

p + opts(axis.text.y = theme_text(family="ComputerModern"))

but there are three questions (that doesn't work):

Firstly, what does the "family" parameter want? I've looked at how it
works in Grid but I'm a bit confused about that. I tried use the
Type1Font command, pointing to the relevant files, but AFAIK
ComputerModern should already be defined. But ggplot2 definitely
doesn't like that. Can one only use the Adobe files?

Secondly, is there a way to change the font family for the whole theme
(akin to base_size?) or does one have to change all the textual
elements separately? That would be pretty awesome.

Finally, is there a way to get ggplot2 to embed the relevant fonts (if
not I think I'll just use a raster format, like PNG)

FWIW, I've had good success at getting the font size right by using
base_size and specifying the actual width and height that I want the
final graphic.

I know that fonts are a pain in the arse, so hope I haven't opened a
can of worms here.

Thanks,
James

hadley wickham

unread,
Jan 19, 2009, 5:29:17 PM1/19/09
to James Howison, ggp...@googlegroups.com
On Mon, Jan 19, 2009 at 4:20 PM, James Howison
<ja...@freelancepropaganda.com> wrote:
>
> I'm tidying up my dissertation and I'd like to use the same font in
> ggplot2 created graphics as I'm using in LaTeX, Computer Modern.
>
> I think I've found the appropriate function to alter the theme in the
> "Polishing" chapter, ie theme_text
>
> p + opts(axis.text.y = theme_text(family="ComputerModern"))
>
> but there are three questions (that doesn't work):
>
> Firstly, what does the "family" parameter want? I've looked at how it
> works in Grid but I'm a bit confused about that. I tried use the
> Type1Font command, pointing to the relevant files, but AFAIK
> ComputerModern should already be defined. But ggplot2 definitely
> doesn't like that. Can one only use the Adobe files?

Family wants what grid wants (for the font family parameter), and
that's described in:

Paul Murrell and Brian Ripley. Non-standard fonts in PostScript and
PDF graphics. R News, 6(2):41-47, May 2006.
http://CRAN.R-project.org/doc/Rnews/Rnews_2006-2.pdf

The article includes an example of computer modern, and embedding the
fonts in the output pdf.

> Secondly, is there a way to change the font family for the whole theme
> (akin to base_size?) or does one have to change all the textual
> elements separately? That would be pretty awesome.

Unfortunately, you have to change them all separately. I'd suggest
you take a look at the code in theme_grey and modify it yourself -
I'll do the same for a future version of ggplot2.

Regards,

Hadley

--
http://had.co.nz/

James Howison

unread,
Jan 20, 2009, 4:07:35 PM1/20/09
to ggp...@googlegroups.com
Thanks Hadley, that worked, but with one quirk.

In short I copied and pasted the theme_bw(), and added a base_family
parameter, them added family = base_family to each theme_text setting.

I think followed the instructions in the pdf you provided, but used
absolute paths and moved the cmsyase (which isn't in the cm-lgc TeX
package, see [1]) into the same folder as the .afm files, then moved
them to a location I made up.

CM <- Type1Font("CM",
c(paste("/Users/james/Library/RFonts/cm-lgc/fonts/afm/public/cm-lgc/",
c("fcmr8a.afm", "fcmb8a.afm",
"fcmri8a.afm", "fcmbi8a.afm", "cmsyase.afm"),
sep="")))

Then I did

pdfFonts(CM=CM)

I was then able to do

theme_set(my_theme(base_family="CM"))

This works if I use a pdf device or ggsave, eg:

data(mtcars)
p <- ggplot(data = mtcars, aes(wt,mpg)) + geom_point()

pdf(file="cm-test.pdf")
p
dev.off()

OR

ggsave(filename="cm-test.pdf")

but not when I'm drawing to the screen, then I get this error:

Error in grid.Call("L_textBounds", as.graphicsAnnot(x$label), x$x, x
$y, :
Polygon edge not found
In addition: Warning messages:
1: In grid.Call("L_textBounds", as.graphicsAnnot(x$label), x$x, x$y, :
no font could be found for family "CM"
2: In grid.Call("L_textBounds", as.graphicsAnnot(x$label), x$x, x$y, :
no font could be found for family "CM"

I'm sure that this is related to the Quartz device, but quartzFonts
seems to work differently than pdfFonts ... Anyway, that's not a big
issue for me; but it was confusing for a while there, pre-defined font
families (eg "serif" and "mono" :) work.

One has to remember to embed the fonts, as described in the article
you linked to.

Thanks,
James

[1]: http://www.stat.auckland.ac.nz/~paul/R/CM/CMR.html

hadley wickham

unread,
Jan 20, 2009, 11:22:06 PM1/20/09
to James Howison, ggp...@googlegroups.com
On Tue, Jan 20, 2009 at 3:07 PM, James Howison
<ja...@freelancepropaganda.com> wrote:
>
> Thanks Hadley, that worked, but with one quirk.
>
> In short I copied and pasted the theme_bw(), and added a base_family
> parameter, them added family = base_family to each theme_text setting.

Great! And thanks for writing your process up - I'm sure it will be
helpful for others in the future.

>
> Error in grid.Call("L_textBounds", as.graphicsAnnot(x$label), x$x, x
> $y, :
> Polygon edge not found
> In addition: Warning messages:

I think you're correct in your diagnosis of this problem - the quartz
fonts and pdf fonts are quite independent.

Reply all
Reply to author
Forward
0 new messages