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

ggtree物种标签颜色设置失败问题求助

ggtree设置物种标签颜色失败问题解决

问题描述

使用ggtree为物种名称标签按Block分组设置颜色时遇到报错,尝试两种方式均失败:直接引用列名提示找不到对象,引用数据框列则提示长度不匹配。仅输入6个分属3个Block的物种,却提示需要对应10条数据的颜色映射。

可复现代码

# 加载所需包
library(metacoder)
library(taxize)
library(ggtree)
library(tidyverse)
library(rentrez)

my.db<-data.frame(
  ID=c(1436140, 214434, 426669, 216727, 1223577, 1752675),
  Name=c("Ulnaria acus","Fragilaria pinnata","Ulnaria ulna", "Achnanthes", "Gomphonema clavatum", "Navicula cincta"),
  Blocks=c("Block1","Block2", "Block3", "Block2", "Block3", "Block3"))

class<- classification(my.db$ID, db="ncbi")
class.tree<-class2tree(class, check = TRUE)
tree<-ggtree(tr= class.tree$phylo,
       layout = "fan",
       linewidth=0.15)+
  geom_tree(linewidth=0.15)+
  geom_tiplab(aes(colour=Blocks),
    linewidth = 0,
              size=2.5)
tree

报错信息

第一次报错

Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
> tree
Error in `label_geom()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 5th layer.
Caused by error:
! object 'Blocks' not found
Run `rlang::last_trace()` to see where the error occurred.

修改代码后报错

将geom_tiplab参数改为aes(colour=my.db$Blocks)后,报错:

Error in `label_geom()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 5th layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (10).
✖ Fix the following mappings: `colour`.
Run `rlang::last_trace()` to see where the error occurred.

回溯信息

运行rlang::last_trace()得到:

Backtrace:
     ▆
 1. ├─base (local) `&lt;fn&gt;`(x)
 2. └─ggplot2:::print.ggplot(x)
 3.   ├─ggplot2::ggplot_build(x)
 4.   └─ggplot2:::ggplot_build.ggplot(x)
 5.     └─ggplot2:::by_layer(...)
 6.       ├─rlang::try_fetch(...)
 7.       │ ├─base::tryCatch(...)
 8.       │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
 9.       │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
 10.       │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
 11.       │ └─base::withCallingHandlers(...)
 12.       └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
 13.         └─l$compute_aesthetics(d, plot)
 14.           └─ggplot2 (local) compute_aesthetics(...,, self = self)
 15.             └─ggplot2:::check_aesthetics(evaled, n)

问题原因

  1. 分组列不在绘图数据中:ggtree生成的树包含分类层级的所有节点(如界、门、纲等),不止输入的6个物种,总节点数为10,因此直接引用Blocks会提示找不到对象。
  2. 长度不匹配:my.db只有6行数据,而树的绘图数据有10行,直接引用my.db$Blocks会导致颜色映射长度不匹配。

解决方案

修改后的代码

# 加载所需包
library(metacoder)
library(taxize)
library(ggtree)
library(tidyverse)
library(rentrez)

my.db<-data.frame(
  ID=c(1436140, 214434, 426669, 216727, 1223577, 1752675),
  Name=c("Ulnaria acus","Fragilaria pinnata","Ulnaria ulna", "Achnanthes", "Gomphonema clavatum", "Navicula cincta"),
  Blocks=c("Block1","Block2", "Block3", "Block2", "Block3", "Block3"))

class<- classification(my.db$ID, db="ncbi")
class.tree<-class2tree(class, check = TRUE)

# 构建树并关联外部分组数据
tree<-ggtree(tr= class.tree$phylo,
       layout = "fan",
       linewidth=0.15) +
  # 用%<+%将my.db的信息合并到树数据中,自动匹配tip标签与Name列
  %<+% my.db +
  geom_tiplab(aes(colour=Blocks), size=2.5)
tree

关键说明

  • 使用ggtree专属的%<+%操作符,将外部数据框my.db的信息合并到树的绘图数据中,确保Blocks列能被geom_tiplab识别。
  • 移除了重复的geom_tree()调用,因为ggtree()已经包含树的绘制,避免重复设置y轴导致的警告。
  • 确保my.db的Name列与树的tip标签完全一致,否则需要手动指定匹配列(如%<+% my.db %>% mutate(label = Name))。

内容的提问来源于stack exchange,提问作者Ginko-Mitten

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 13:47:06