如何在 R 中像 ggdensitree 一样堆叠绘制的系统发育树,但具有彩色机制

问题描述 投票:0回答:1

我想绘制一个系统发育树的样本,其中的分支被“绘制”来代表政权,类似于 phytools 包中的plotSimmap的结果:

enter image description here

但是,我不想绘制一棵树,而是想在单个可视化中将多棵树堆叠在一起,类似于 ggtree 包中的 ggdensitree 的行为:

enter image description here

目标是拥有一个堆叠图,其中每棵树都保留其特定于政权的树枝颜色。有没有办法在 R 中实现这一点,可能使用 ggtree、ggplot2 或其他方法?

编辑

可重现示例: 例如,我们可以这样模拟 25 棵树的样本:

library(ape)
library(phytools)
library(ggtree)

set.seed(123)

trees_with_regimes <- list()

# Generate trees with mapped regimes
for (i in 1:25) {

  tree <- rtree(5)   # 5 tips
  
  # Define regimes, "A" and "B"
  regimes <- sample(c("A", "B"), size = 5, replace = TRUE)
  names(regimes) <- tree$tip.label # Assign regimes to tip labels
  
  # Map regimes onto the tree
  mapped_tree <- make.simmap(tree, regimes, model = "ER", nsim = 1)
  
  # Store the mapped tree
  trees_with_regimes[[i]] <- mapped_tree
}

如果我们分别绘制每棵树,它们将以不同的颜色显示状态:

# works also with "plot" instead of "plotSimmap"
plotSimmap(trees_with_regimes[[1]], lwd  = 7, fsize = 2)

enter image description here

但是在绘制整个树样本时,政权颜色会丢失:

# or any other function for plotting several stacked trees
ggdensitree(trees_with_regimes)

This doesn't look nice because the trees were randomly generated

我希望最后一个图包含两种制度的不同颜色。

预先感谢您的任何建议!

r plot phylogeny ggtree ape-phylo
1个回答
0
投票

您可以使用

add
中的
plotSimmap
参数(一个逻辑值,指示是否将绘制的树添加到当前绘图 (TRUE) 或创建新绘图(FALSE,默认值))来绘制 25 个 Simmap彼此重叠,同时仍保留颜色。

library(ape)
#install.packages("phytools")
library(phytools)
library(ggtree)
library(ggplot2)
library(tidytree)
library(dplyr)

set.seed(123)
trees_with_regimes <- list()
# Generate trees with mapped regimes
for (i in 1:25) {
  
  tree <- rtree(5)   # 5 tips
  
  # Define regimes, "A" and "B"
  regimes <- sample(c("A", "B"), size = 5, replace = TRUE)
  names(regimes) <- tree$tip.label # Assign regimes to tip labels
  
  # Map regimes onto the tree
  mapped_tree <- make.simmap(tree, regimes, model = "ER", nsim = 1)
  
  # Store the mapped tree
  trees_with_regimes[[i]] <- mapped_tree
}

# Function to plot multiple simmap trees stacked
plot_stacked_simmaps <- function(trees, 
                                 lwd = 7, # line width
                                 fsize = 2, # relative label font size
                                 alpha = 0.5, # alpha of additional plots
                                 colors = NULL, 
                                 showLabels = T) { # show Tip Labels or not
  # If no colors provided, use default blue and red
  if(is.null(colors)) {
    colors <- c("A" = "blue", "B" = "red")
  }
  
  # Plot the first tree normally
  plotSimmap(trees[[1]], lwd = lwd, fsize = fsize, colors = colors, ftype = if(!showLabels) "off" else "reg")
  
  # Add subsequent trees with lower opacity
  if(length(trees) > 1) {
    for(i in 2:length(trees)) {
      # Colors with transparency
      transparent_colors <- sapply(colors, function(x) {
        rgb_vals <- col2rgb(x)
        rgb(rgb_vals[1], rgb_vals[2], rgb_vals[3], alpha = alpha * 255, maxColorValue = 255)
      })
      
      # Add tree to existing plot
      plotSimmap(trees[[i]], 
                 add = TRUE, 
                 lwd = lwd, 
                 fsize = fsize,
                 colors = transparent_colors,
                 ftype = if(!showLabels) "off" else "reg")
    }
  }
}

plot_stacked_simmaps(trees_with_regimes, lwd = 7, fsize = 2, showLabels = T)

没有标签的资源

out

© www.soinside.com 2019 - 2024. All rights reserved.