Wednesday, October 3, 2012

xkcd tree plot: part IV

I just posted my code for plotting xkcd style phylogenetic trees. The code uses tips from a stackoverflow.com discussion thread (initiated by my colleague Jarrett Byrnes), as well as a slightly modified version of an entire function posted by user295691.

The function is available on my phytools page (direct link to the code here).

**A few preliminary steps are required before the function will work!**

1. Install the package 'extrafont' and its dependencies.

2. Download and install the xkcd font, xkcd.ttf. The procedure for adding new fonts will depend on your operating system. For Windows 7, go Start → Control Panel → Appearance and Personalization → Fonts and then just drag & drop the .ttf file into this window.

3. At least in Windows, you will have to download and install Ghostscript. This allows you to embed the xkcd font into the PDF in which the tree is outputted.

The input arguments for xkcdTree are as follows: tree, your R "phylo" object; file, your filename for output (xkcdTree saves your plotted tree as a PDF); gsPath, the complete path to the Ghostscript binary (for a gs9.06 in Windows 7, the default should work); fsize, lwd, and color should be relatively self-explanatory; finally jitter is a scalar for the uncertainty to add (more is more wiggly), and waver is a vector with the horizontal and vertical relative 'wavelength' of the uncertainty (smaller numbers result in a shorter wavelength). Note that my previous example (here) has much more jitter than the default.

Let's try it:
> library(phytools)
Loading required package: ape
...
> source("xkcdTree.R")
> tree<-pbtree(n=40)
> xkcdTree(tree,"testTree.pdf",color="black",fsize=1.5)
Loading required package: extrafont
...
That's it!

5 comments:

  1. There is also a small bug in this were the argument 'lwd' doesn't do anything. Link to fixed version here.

    ReplyDelete
  2. Hi Liam,
    Sorry to bother you again but after I tried this I encontered

    xkcdTree(tree,"testTree.pdf", gsPath="C:/Program Files/gs/gs9.06/bin/gswin32c.exe", color="black",fsize=1.5)
    **** NOTE: use in Windows requires the installation of Ghostscript to**** embed 'xkcd' font in PDF plot
    **** NOTE: an 'embed_fonts' error most likely means that Ghostscript
    **** is not installed or that the path is incorrect
    Warning messages:
    1: In pdf(file, family = "xkcd", width = dim[1], height = dim[2]) :
    unknown AFM entity encountered
    2: In pdf(file, family = "xkcd", width = dim[1], height = dim[2]) :
    unknown AFM entity encountered
    3: In pdf(file, family = "xkcd", width = dim[1], height = dim[2]) :
    unknown AFM entity encountered
    4: In pdf(file, family = "xkcd", width = dim[1], height = dim[2]) :
    unknown AFM entity encountered


    I added the extrafont, the xkcd font and installed Ghostscript. Is there a way I could check if something else isn't working?
    Thanks.

    ReplyDelete
  3. Hi Andres.

    If it worked, it should have created your plot in a PDF. Did it? Those errors might not be an issue.

    - Liam

    ReplyDelete
  4. Hi Liam,

    OK. It just noticed it created a .pdf as expected. I thought something was wrong since the R Graphics device was just grey but that's a minor issue if the pdf was indeed created. Sorry for my confusion.

    ReplyDelete
    Replies
    1. Great. As you know, Andres, 'phytools' is a work in progress - so there will be bugs. Nonetheless, I'm glad to hear that it works! Thanks for the report. - Liam

      Delete