R 中的 ggTree 和 Deeptime 自定义帮助 - 在树上创建范围条和灰色参考线

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

嗨,我已经制作了时间树,每个分类单元都有一个根时间和年龄范围。我已经使用

strap
包和
geoscalephylo
绘制了此图,它看起来不错,但我想要更多的自定义。

我想要这样的东西:

Range Bars and grey background guides used in strap

这是迄今为止我的代码:

x <- ggtree(VTree, root.position = -VTree$root.time) %<+% TheroData
x2 <- x + geom_rootpoint(color="red", size=3, shape = 19)
x3 <- x2 + coord_geo(
    xlim = c(-250, -50), ylim = c(-2, Ntip(VTree)),
    pos = list("bottom", "bottom"),
    dat = list("stages","periods"), abbrv = list(T, F),
    size = list(4, 5), neg = TRUE, center_end_labels = TRUE) +
  scale_x_continuous(breaks = -rev(epochs$max_age),
                     labels = rev(epochs$max_age)) +
  theme_tree2() +
  theme(plot.margin = margin(7, 11, 7, 11))
x3

TimeTree customised using ggTree and Deeptime

所以我尝试了

geom_ranges
包中的
guides
Deeptime
,文档在这里:https://williamgearty.com/deeptime/

但我不知道如何处理它。我的范围数据样本位于我的树对象中

VTree$ranges

这是来自

VTree$ranges

的一些示例数据
                                      FAD       LAD
Eodromaeus_murphi               233.44617 231.99012
Eoraptor_lunensis               235.84630 233.60026
Gnathovorax_cabreirai           235.99430 229.72512
Herrerasaurus_ischigualastensis 236.06716 227.52685
Saturnalia_tupiniquim           228.54899 219.22073
Coelophysis_bauri               218.82258 204.64334
Liliensternus_liliensterni      208.23313 208.14574
Plateosaurus_trossingensis      215.64771 212.86567
Tawa_hallae                     212.61870 211.49031
Massospondylus_carinatus        200.70891 197.74069

我确实看到了一种使用此方法的方法,但我无法复制修复:

tree = rtree(3) %>% as.treedata %>% as_tibble %>% 
  mutate(number = 1:5,
        range = lapply(number, function(x) c(-0.1, 0.1) + x)) %>% 
  as.treedata

# plot the tree and add red bars with geom_bar()
ggtree(tree)  + 
  geom_range("number", range='range', color="red", size = 3, alpha = 0.3) + 
  theme_tree2()

我感谢提供的任何帮助。

r ggplot2 phylogeny ggtree
1个回答
0
投票

我会这样做。我将

geom_rect()
与您的
VTree$ranges
数据一起使用:)

using<-function(...) {
  libs<-unlist(list(...))
  req<-unlist(lapply(libs,require,character.only=TRUE))
  need<-libs[req==FALSE]
  if(length(need)>0){ 
    install.packages(need)
    lapply(need,require,character.only=TRUE)
  }
}
using("tidytree", "BiocManager", "ggtree", "purrr", "gsloid","divDyn","deeptime","ggplot2","dplyr")

VTree <- rtree(20) 
VTree$root.time <- 250  # Assume root time is 250 million years ago

# Sample data for the ranges (FAD and LAD) for each taxon
VTree$ranges <- data.frame(
  FAD = c(233.44617, 235.84630, 235.99430, 236.06716, 228.54899, 218.82258),
  LAD = c(231.99012, 233.60026, 229.72512, 227.52685, 219.22073, 204.64334),
  row.names = c("Eodromaeus_murphi", "Eoraptor_lunensis", "Gnathovorax_cabreirai", 
                "Herrerasaurus_ischigualastensis", "Saturnalia_tupiniquim", 
                "Coelophysis_bauri")
) 

VTree$edge.length <- VTree$edge.length * 10  # Make all branch lengths twice as long

TheroData <- data.frame(
  taxon = rownames(VTree$ranges),
  stringsAsFactors = FALSE
)

epochs <- data.frame(
  max_age = seq(0, 250, by = 25),  # Example ages in millions of years
  row.names = c("Epoch1", "Epoch2", "Epoch3", "Epoch4", "Epoch5", "Epoch6", "Epoch7", "Epoch8", "Epoch9", "Epoch10", "Epoch11")
)

# Create the time tree with root point
ggtree(VTree, root.position = -VTree$root.time) %<+% TheroData +
  geom_rootpoint(color = "red",
                 size = 3,
                 shape = 19)  +
  coord_geo(
    xlim = c(-250, -50),
    ylim = c(-2, Ntip(VTree)),
    pos = list("bottom", "bottom"),
    dat = list("stages", "periods"),
    abbrv = list(T, F),
    size = list(2, 3), # adjusted scaling
    neg = TRUE,
    center_end_labels = TRUE
  ) +
  
  scale_x_continuous(breaks = -rev(epochs$max_age),
                     labels = rev(epochs$max_age)) +
  theme_tree2() +
  theme(plot.margin = unit(c(7, 11, 7, 11), "pt")) +
  
  geom_rect(
    data = VTree$ranges,
    inherit.aes = F,
    aes(
      xmin = -FAD,# Take FAD
      xmax = -LAD,# Take LAD
      ymin = -90,# set min-height of grey bars to extremes, so it covers the whole plot
      ymax = 800 # set max height
    ),
    fill = "grey",
    alpha = 0.2
  ) 

out

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