【问题标题】:How do I save a phytools phenogram as an image?如何将 phytools 表形图保存为图像?
【发布时间】:2021-11-05 16:07:23
【问题描述】:

我正在使用 R 包 phytools 中的 phenogram() 函数沿相对时间 (x) 和表型轴绘制系统发育图,在本例中为年平均温度 (y)。它显示在绘图窗口中,但它似乎不作为绘图对象存在,我可以使用 ggsave 在外部将其保存为图像。

这是主要的代码和情节。如有必要,我可以提供一个最小可重现的示例,但它的代码很多,所以我希望这可能是一个非常容易和明显的解决方法。

phenogram(tree_sub, bio01, colors = cols, fsize = 0.8, ftype = "i",
          xlab = ("Relative Time"), ylab = "Mean Annual Temperature (°C)",
          spread.labels = TRUE, spread.cost = c(1, 0))

ggsave(filename = "../output/APCT_phenogram_5Nov2021.png", plot = last_plot(), 
       width = 25, height = 16, units = "cm", dpi = 300)

使用plot = last_plot() 会保存我之前使用 ggtree 绘制的非表型树。将我的表形图分配给一个名为“tree_plot”的对象,然后在 ggsave 中调用它会产生错误:

Error: $ operator is invalid for atomic vectors

这似乎是因为当我将表象分配给一个对象时,它只保存了树中每个物种的 x 和 y 坐标(即不是树拓扑、分支长度、轴标签、颜色等)同样,使用png() 也不起作用——它只会生成一个点图,其中的 x 和 y 坐标在 tree_plot 中列出。

tree_plot <- phenogram(tree_sub, bio01, colors = cols, fsize = 0.8, ftype = "i",
             xlab = ("Relative Time"), ylab = "Mean Annual Temperature (°C)",
             spread.labels = TRUE, spread.cost = c(1, 0))

png(filename = "../output/APCT_phenogram_5Nov2021.png",  width = 25, 
    height = 16, units = "cm", res = 300)

plot(tree_plot)

dev.off()

有谁知道 R 在显示图表时理解我的表象图是什么类型的对象(如下所示),以及我如何保存它,而不是物理单击 RStudio 中的导出菜单?

this is what the plot that I'm trying to save looks like

【问题讨论】:

    标签: r ggplot2 phylogeny ggsave


    【解决方案1】:

    如果有人遇到这个问题,我是在 phytools 的创建者 Revell 博士的帮助下想出来的。事实证明,我几乎只在 ggplot2 中工作过,我真的不知道如何正确使用 png()!

    应该是这样的:

    png(filename = "../output/APCT_phenogram_5Nov2021.png",  width = 25, 
        height = 16, units = "cm", res = 300)
    
    phenogram(tree_sub, bio01, colors = cols, fsize = 0.8, ftype = "i",
              xlab = ("Relative Time"), ylab = "Mean Annual Temperature (°C)",
              spread.labels = TRUE, spread.cost = c(1, 0))
    
    dev.off()
    

    所以制作一个空的 PNG,输入有关它的所有信息,然后绘制您要绘制的任何内容,然后关闭图形设备。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      相关资源
      最近更新 更多