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

ggplot2绘制同位素双图报错求助:geom_errorbarh缺失y美学

问题:ggplot2绘制化合物稳定同位素双图时误差棒报错无法显示

问题描述

使用ggplot2重绘化合物稳定同位素双图时,误差棒无法显示,运行代码触发报错:Error: geom_errorbarh requires the following missing aesthetics: y,同时收到警告:Ignoring unknown aesthetics: x

原代码

dev.new()
ggplot() +
  geom_point(data = conf_intervals, aes(y = mean_ALA, x = mean_LIN, shape=trophic, 
                                        color=feeding_type_2,
                                        size = 3, 
                                        alpha = 0.5)) + scale_shape_manual("Trophic level", values = c(15:19)) +
geom_errorbar(data = conf_intervals,aes(mean_ALA, ymin = mean_ALA - se_ALA,
                                        ymax = mean_ALA + se_ALA,)) +
geom_errorbarh(data = conf_intervals,aes(mean_LIN, ymin = mean_LIN - se_LIN,
                                         ymax = mean_LIN + se_LIN)) +
geom_point(data = CSIA_inverts_basal, aes(y=ALA.d13C, x=LIN.d13C, color=feeding_type_2))
  labs (title="Biplot of compound stable isotopes- Centroids with 95 % CI", subtitle="LIN VS. ALA",
        y=expression({delta}^13*C[ALA]~'‰'~VPDB),
        x=expression({delta}^13*C[LIN]~'‰'~VPDB)) + 
  # guides(color = FALSE, shape = FALSE) +
  theme_classic() 

报错信息

Error: geom_errorbarh requires the following missing aesthetics: y
Run rlang::last_error() to see where the error occurred.
In addition: Warning message:
Ignoring unknown aesthetics: x

示例数据

# CSIA_inverts_basal 前6行数据
dput(head(CSIA_inverts_basal))
structure(list(d13C.VPDB = c(-35.10487341, -34.85465717, -34.67216423, 
-34.06032315, -33.68548439, -33.4811546), d15.NAIR = c(-6.321847159, 
-5.384989361, -2.638749276, -4.986045928, -5.946279778, -6.648526348
), ALA.d13C = c(-43.2375195, -44.77813854, -42.1921855, -41.58363894, 
-39.156857, -40.33135344), LIN.d13C = c(-40.864145, -42.32043061, 
-41.4247005, -36.08156681, -39.45744387, -37.76516617), combi = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), levels = c("epilithon", "fresh.leaves", 
"gammarus", "grazing.ephemeroptera", "predatory.plecoptera", 
"salmonid.eyes", "shreddering.plecoptera", "submerged.leaves"
), class = "factor"), feeding_type = c("epilithon", "epilithon", 
"epilithon", "epilithon", "epilithon", "epilithon"), sampling.time = c("summer", 
"fall", "summer", "fall", "fall", "fall"), year = c(2018L, 2016L, 
2018L, 2016L, 2016L, 2016L), split = structure(c(2L, 2L, 2L, 
2L, 2L, 2L), levels = c("consumer", "resource"), class = "factor"), 
    split_2 = c("epilithon", "epilithon", "epilithon", "epilithon", 
    "epilithon", "epilithon"), split_3 = c("epilithon", "epilithon", 
    "epilithon", "epilithon", "epilithon", "epilithon"), feeding_type_2 = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), levels = c("Epilithon", "Fresh leaves", 
    "Grazer", "Salmonid (Eyes)", "Predator", "Submerged leaves", 
    "Shredder"), class = "factor"), trophic = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), levels = c("Base", "Non-predatory invertebrate", 
    "Predatory invertebrate", "Predator"), class = "factor")), row.names = 2:7, class = "data.frame")

# conf_intervals 数据
dput(conf_intervals)
structure(list(trophic = structure(c(1L, 1L, 1L, 2L, 2L, 3L, 
4L), levels = c("Base", "Non-predatory invertebrate", "Predatory invertebrate", 
"Predator"), class = "factor"), feeding_type_2 = structure(c(1L, 
2L, 6L, 3L, 7L, 5L, 4L), levels = c("Epilithon", "Fresh leaves", 
"Grazer", "Salmonid (Eyes)", "Predator", "Submerged leaves", 
"Shredder"), class = "factor"), mean_ALA = c(-42.1, -39.7, -38.7, 
-45.7, -40.3, -42.8, -42.7), mean_LIN = c(-39.2, -40, -37.2, 
-40.8, -35.9, -36.7, -37.9), se_ALA = c(1.1, 1.1, 1.1, 2.2, 1.2, 
1.9, 0.4), se_LIN = c(1.1, 1.1, 0.6, 1.8, 0.9, 1.3, 0.6), N_ALA = c(12L, 
14L, 10L, 9L, 14L, 7L, 17L), LIN_N = c(12L, 14L, 10L, 9L, 14L, 
7L, 17L)), class = "data.frame", row.names = c(NA, -7L))

问题原因及修正方案

核心问题分析

  1. geom_errorbar参数缺失:垂直误差棒需要指定x参数定位水平位置,原代码仅传入mean_ALA,未明确x=mean_LIN,导致警告。
  2. geom_errorbarh参数错误:水平误差棒需用xmin/xmax定义误差范围(而非ymin/ymax),同时必须指定y参数定位垂直位置,原代码参数用反且缺失y,触发报错。

修正后的代码

dev.new()
ggplot() +
  # 绘制质心点(将固定样式参数移出aes)
  geom_point(data = conf_intervals, 
             aes(y = mean_ALA, x = mean_LIN, shape = trophic, color = feeding_type_2),
             size = 3, alpha = 0.5) +
  scale_shape_manual("Trophic level", values = c(15:19)) +
  # 垂直误差棒(对应ALA的y轴误差)
  geom_errorbar(data = conf_intervals,
                aes(x = mean_LIN, y = mean_ALA, 
                    ymin = mean_ALA - se_ALA, ymax = mean_ALA + se_ALA),
                width = 0.2) +
  # 水平误差棒(对应LIN的x轴误差)
  geom_errorbarh(data = conf_intervals,
                 aes(y = mean_ALA, x = mean_LIN,
                     xmin = mean_LIN - se_LIN, xmax = mean_LIN + se_LIN),
                 height = 0.2) +
  # 绘制原始数据点
  geom_point(data = CSIA_inverts_basal, aes(y = ALA.d13C, x = LIN.d13C, color = feeding_type_2)) +
  # 图表标签设置
  labs(title = "化合物稳定同位素双图 - 带95%置信区间的质心",
       subtitle = "LIN VS. ALA",
       y = expression({delta}^13*C[ALA]~'‰'~VPDB),
       x = expression({delta}^13*C[LIN]~'‰'~VPDB)) + 
  # guides(color = FALSE, shape = FALSE) +
  theme_classic() 

关键修正点说明

  • 将size=3、alpha=0.5移出aes(),这两个是固定样式,无需映射到数据变量,避免不必要的警告。
  • 给geom_errorbar补充x=mean_LIN,明确垂直误差棒的水平位置。
  • 给geom_errorbarh替换ymin/ymax为xmin/xmax,并添加y=mean_ALA定位垂直位置,解决报错。
  • 给误差棒添加width(垂直)和height(水平)参数,优化视觉呈现。

内容的提问来源于stack exchange,提问作者Nadiine El Nino

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 13:10:26