导出高分辨率、节点标记的系统发育树

Exporting a high-res, node-labelled phylogenetic tree

很久了reader,第一次提问!

所以我有一个具有 18 个提示 (A-Q) 和 17 个内部节点的系统发育树。内部节点用一些彩色饼图标记如下:

    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)

我正在尝试导出这棵树的高分辨率 PNG 或 Tiff 图像,但遇到了问题。

原代码:

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()

导致以下错误:

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

尝试的解决方案 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()

这给了我一个不同的错误:

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

尝试的解决方案 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()

这确实生成了 PNG,但名称为 'plot' 而不是“SpeciesTree_withBins”,生成的图像被严重压扁。

Squashedplot

我不确定如何解决这个问题。有人可以帮我导出一个高分辨率的节点标记树,它没有被压扁(最好使用正确的文件名)吗?

这似乎是分辨率问题。请注意,我无法重现您的示例,因此我在这里可能会稍微偏离轨道(即,我不知道 treeCairo 函数的来源 - 我还假设您正在使用phytools::to.matrix).

您的解决方案 2 似乎运行良好,您可以使用 png 而不是 Cairo 更改文件名,并且可以使用 height 更改绘图尺寸] 和 width 中的参数 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()