2

I have several phylogenetic trees imported into R from Newick format. I am using the ape package to plot the trees with plot.phylo command. I would like to be able to change the font family (not only the size, which I can do with cex, or color with col) of the tip labels to monospace. The plot command does take family argument, but nothing happens when I pass family="mono". I tried including it in par with no success either.

library(ape)
tr <- rtree(10)
plot(tr)

gives me the same as

plot(tr, family="mono")

And I would like to see a change in font.

EDIT: The font-family specification seems to work when saving graphic to png, but not devSVG. How can I save updated font to SVG?

C_Z_
  • 7,427
  • 5
  • 44
  • 81
antass
  • 99
  • 7
  • Which documentation are you looking at? You should be looking at `?plot.phylo` rather than `?plot` ... if you don't have luck here you might try the r-sig-phylo r-project.org mailing list ... – Ben Bolker Mar 01 '12 at 17:12
  • I searched both. None have any parameters to change font family/face. But it seems you can pass any graphical parameters (thanks to `... =`). Also, `family` works for regular plots (scatterplots, etc), but not for trees. Thanks for the mailing list recommendation! – antass Mar 01 '12 at 17:46
  • unfortunately, the fact that you can pass a parameter in the `...` slot doesn't necessarily mean that the function will do anything useful with it ... – Ben Bolker Mar 01 '12 at 18:07
  • that's exactly how it looks - for an object of class `phylo` it does nothing :/ – antass Mar 01 '12 at 19:04
  • looks like you may have gotten your answer via the `tiplabels` command, on r-sig-phylo. If you want to write that up as an answer here, you're allowed (encouraged) to answer your own questions so that future searchers will find the answer ... – Ben Bolker Mar 01 '12 at 20:31
  • unfortunately, `tiplabels` does not help either. it works in simple examples, but it doesn't work for my tree. It may be that the font gets "lost" somewhere on its way to `SVG` format. I can't view it normally in X11() because the labels overlap and I just see a black mess. – antass Mar 01 '12 at 21:07
  • ok, so I can confirm that the problem is with `devSVG`. If I set `family="mono"` in `par` and save the tree to a `png`, I can see the Courier New font. The problem remains, though: how Do I change the font and save to `SVG`? I'll refine my question to reflect device dependency. – antass Mar 01 '12 at 21:09
  • try `svg` in the `Cairo` package instead ... ? (I can confirm that it works for a trivial example.) – Ben Bolker Mar 01 '12 at 21:44
  • yes, that's exactly what I have tried and it worked - I am just not allowed to post my own answer for another hour ;) I have it typed in and ready to be posted! – antass Mar 01 '12 at 23:07
  • and actually, it's weird, but `Cairo` and `cairo` are two different packages, and `svg` sits in `cairo` under `grDevices`. – antass Mar 01 '12 at 23:08

1 Answers1

2

Finally, a success!

In order to be able to manipulate font-family when saving graphics in SVG format, I had to use the package grDevices and method cairo:

library(cairo)
svg(filename = file, width = width, height = height, family = "mono")

which allows setting the family argument.

For future reference, what did NOT work was:

devSVG(file, width, height) and then setting family in par or plot,

and Cairo(file, width, height, type="svg") with family in par or plot

antass
  • 99
  • 7