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

使用Ape包pic函数结合ggplot绘制系统发育独立对比图报错求助

问题解决:PIC结果与ggplot可视化结合

错误原因

你遇到的错误是因为ggplot调用时使用了原始数据集datos(134行),但icx和icy是系统发育独立对比(PIC)结果,其长度等于树的节点数(即物种数-1),两者长度不匹配,导致美学映射校验失败。

修正方案

1. 为PIC结果创建单独的数据框

将icx和icy合并为一个新的数据框,作为ggplot的数据源:

pic_df <- data.frame(icx = icx, icy = icy)

2. 正确绘制PIC散点图

使用新的数据框绘制:

ggplot(data = pic_df, aes(x = icx, y = icy)) +
  geom_jitter() +
  labs(x = "Tarsus Female", y = "Tarso Male", title = "Indep_C_Tarsus") +
  theme_minimal()

3. 修复线性回归绘图的问题

你的代码中abline(fit)是base R函数,无法直接在ggplot图层后使用,需要替换为geom_abline:

ggplot(data = datos) +
  geom_point(aes(x = Log10.Tarso.M, y = Log10.Tarso.H), color = "orange") +
  geom_point(aes(x = Log10.Tarso.H, y = Log10.Tarso.M), color = "lightblue") +
  geom_abline(intercept = coef(fit)[1], slope = coef(fit)[2], color = "red") +
  labs(title = "Scatter plot of Log10.Tarso.M vs Log10.Tarso.H", 
       x = "Log10.Tarso", y = "Log10.Tarso") +
  theme_minimal() +
  theme(legend.position = "none")

完整修正代码

library(phytools)
library(ape)
library(tidyverse)

datos <- read.csv("UpdatedScriptTarsos.csv", header = TRUE)
arbol <- read.nexus("TrueTree.nex")

# 提取变量并命名
x <- setNames(datos[,5], datos[,1])
y <- setNames(datos[,3], datos[,1])

# 绘制原始数据散点图(含回归线)
fit <- lm(y ~ x)
ggplot(data = datos) +
  geom_point(aes(x = Log10.Tarso.M, y = Log10.Tarso.H), color = "orange") +
  geom_point(aes(x = Log10.Tarso.H, y = Log10.Tarso.M), color = "lightblue") +
  geom_abline(intercept = coef(fit)[1], slope = coef(fit)[2], color = "red") +
  labs(title = "Scatter plot of Log10.Tarso.M vs Log10.Tarso.H", 
       x = "Log10.Tarso", y = "Log10.Tarso") +
  theme_minimal() +
  theme(legend.position = "none")

# 输出回归结果
fit
summary(fit)
anova(fit)

# 计算独立对比
icx <- pic(x, arbol)
icy <- pic(y, arbol)

# 为PIC创建数据框并绘图
pic_df <- data.frame(icx = icx, icy = icy)
ggplot(data = pic_df, aes(x = icx, y = icy)) +
  geom_jitter() +
  labs(x = "Tarsus Female", y = "Tarso Male", title = "Indep_C_Tarsus") +
  theme_minimal()

结论

完全可以将ape的PIC分析与ggplot结合使用,不需要寻找替代方案,核心是确保ggplot使用的数据源与美学映射变量的长度一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 17:52:02