Search code examples
rimageexportrstudiophylogeny

Exporting a high-res, node-labelled phylogenetic tree


Long-time reader, first-time asker!

So I have a Phylogenetic tree with 18 tips (A-Q) and 17 internal nodes. The internal nodes are labelled with some coloured pies as follows:

    plot(tree, cex = 0.8, label.offset= 1)
mbv <- c(1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0,0)
nodelabels(pie = mbv, piecol = c("black", "white"), cex = 0.8)

trait <- c(0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1)
names(trait)<-tree$tip.label
tiplabels(pie = to.matrix(trait, sort(unique(trait))), piecol = c("black", "white"), 
          cex = 0.4)

I am trying to export a high-resolution PNG or Tiff image of this tree but am having problems.

Original code:

Cairo(filename='SpeciesTree_withBins.png', type="png", res=300)
par(mar=c(1,1,1,1))
{plot(tree, cex = 0.8, label.offset= 1)
mbv <- c(1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0,0)
nodelabels(pie = mbv, piecol = c("black", "white"), cex = 0.8)
trait <- c(0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1)
names(trait)<-tree$tip.label
tiplabels(pie = to.matrix(trait, sort(unique(trait))), piecol = c("black", "white"), 
          cex = 0.4)}
dev.off()

Which resulted in the following error:

Error in symbols(xpos, ypos, circles = radius, inches = FALSE, add = TRUE,  : 
  plot.new has not been called yet

Attempted solution 1:

Cairo(filename='SpeciesTree_withBins.png', type="png", res=300)
{plot(tree, cex = 0.8, label.offset= 1)
  mbv <- c(1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0,0)
  nodelabels(pie = mbv, piecol = c("black", "white"), cex = 0.8)    
  trait <- c(0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1)
  names(trait)<-tree$tip.label
  tiplabels(pie = to.matrix(trait, sort(unique(trait))), piecol = c("black", "white"), 
            cex = 0.4)}
dev.off()

Which gave me a different error:

 Error in plot.new() : figure margins too large 

Attempted solution 2:

Cairo(filename='SpeciesTree_withBins.png', type="png", res=300)
par(mar=c(1,1,1,1))
{plot(tree, cex = 0.8, label.offset= 1)
mbv <- c(1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0,0)
nodelabels(pie = mbv, piecol = c("black", "white"), cex = 0.8)
trait <- c(0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1)
names(trait)<-tree$tip.label
tiplabels(pie = to.matrix(trait, sort(unique(trait))), piecol = c("black", "white"), 
          cex = 0.4)}
dev.off()

This did produce a PNG but with the name 'plot' rather than'SpeciesTree_withBins' and the resulting image is terribly squashed.

Squashedplot

I am unsure of how to resolve this issue. Can someone please aid me in exporting a high-resolution, node-labelled tree that isn't squashed (and preferably with the correct file name)?


Solution

  • This seems due to a resolution issue. Mind that I could not reproduce your example so I might be slightly off track here (namely, I don't know where the tree and the Cairo functions come from - I also assumed that you're using phytools::to.matrix).

    Your solution 2 seems to be working well, you can change the name of the file by using png rather than Cairo and you can change the plot dimensions by using the height and width arguments in png:

    library(ape)
    library(phytools)
    ## Making a random tree
    tree <- rtree(18)
    
    ## Setting up the png output file
    ## you might want to change the width and height here!
    png(filename = "SpeciesTree_withBins.png", res = 300,
        width = 800, height = 800)
    
    ## The margin definition
    par(mar = c(1,1,1,1))
    
    ## Some tree parameters
    mbv <- c(1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0,0)
    trait <- c(0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1)
    names(trait) <- tree$tip.label
    
    ## Plotting the tree
    plot(tree, cex = 0.8, label.offset= 1)
    nodelabels(pie = mbv, piecol = c("black", "white"), cex = 0.8)
    tiplabels(pie = to.matrix(trait, sort(unique(trait))),
                              piecol = c("black", "white"),cex = 0.4)
    
    ## Saving the file
    dev.off()