You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言中ggTree与Deeptime定制求助:在时间树添加范围条与灰色参考线

古生物时间树定制:添加分类单元范围条与灰色背景参考线

已通过strap与geoscalephylo绘制出包含根时间及各分类单元年龄范围的时间树,现需进一步定制,实现分类单元年龄范围条与灰色背景参考线的效果,尝试使用deeptime包的geom_range但未成功,求解决方案。


当前绘图代码

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

分类单元FAD/LAD数据

VTree$ranges中存储了各分类单元的首次出现时间(FAD)与末次出现时间(LAD):

分类单元FADLAD
Eodromaeus_murphi233.44617231.99012
Eoraptor_lunensis235.84630233.60026
Gnathovorax_cabreirai235.99430229.72512
Herrerasaurus_ischigualastensis236.06716227.52685
Saturnalia_tupiniquim228.54899219.22073
Coelophysis_bauri218.82258204.64334
Liliensternus_liliensterni208.23313208.14574
Plateosaurus_trossingensis215.64771212.86567
Tawa_hallae212.61870211.49031
Massospondylus_carinatus200.70891197.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()

解决方案

核心问题说明

你的时间树采用负数值表示地质时间(根位置设为-VTree$root.time),而VTree$ranges中的FAD/LAD是正数值,因此需要先转换坐标格式,才能匹配绘图体系。

完整实现代码

# 1. 提取树中每个分类单元的y轴位置,匹配ranges的行名
tip_positions <- ggtree::get_taxa_position(x, taxa = rownames(VTree$ranges))

# 2. 整理范围条数据:转换时间为负数值,匹配x轴坐标
range_df <- data.frame(
  taxa = rownames(VTree$ranges),
  y = tip_positions,
  # FAD是更早的时间,对应x轴更小的负数;LAD是更近的时间,对应x轴更大的负数
  x_start = -VTree$ranges$FAD,
  x_end = -VTree$ranges$LAD
)

# 3. 在原有绘图基础上添加范围条与灰色参考线
final_plot <- x3 +
  # 添加分类单元年龄范围条(可调整颜色、粗细、透明度)
  geom_segment(
    data = range_df,
    aes(x = x_start, xend = x_end, y = y, yend = y),
    color = "#2c3e50", size = 2.5, alpha = 0.7
  ) +
  # 添加灰色背景参考线(对应地质时期分界点)
  geom_vline(
    xintercept = -epochs$max_age,
    color = "gray80", linetype = "dashed", size = 0.5
  )

# 查看最终绘图
final_plot

替代方案:使用deeptime的geom_range

如果你坚持使用geom_range,需要调整参数适配垂直树的水平范围条:

# 先把range列转为列表格式(每个元素是c(x_start, x_end))
range_df$range <- lapply(1:nrow(range_df), function(i) c(range_df$x_start[i], range_df$x_end[i]))

# 使用geom_range添加范围条
final_plot <- x3 +
  geom_range(
    data = range_df,
    aes(x = y, range = range),
    color = "#2c3e50", size = 2.5, alpha = 0.7,
    orientation = "y"  # 关键:指定方向为y轴,对应水平范围条
  ) +
  geom_vline(
    xintercept = -epochs$max_age,
    color = "gray80", linetype = "dashed", size = 0.5
  )

注意事项

  • 确保VTree$ranges的行名与树的分类单元标签完全一致,否则会出现位置匹配错误。
  • 可根据需求调整范围条的颜色、粗细、透明度,以及参考线的样式。

内容的提问来源于stack exchange,提问作者Aamir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 04:27:36