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

ggplot2调用stat_smooth拟合非线性标准曲线出现p参数缺失报错

错误原因排查与修复方案

核心错误原因

报错的根本原因是stat_smooth()的method参数配置错误:

  • 你选用的method = "nlm"不符合调用要求:nlm是非线性最小二乘优化函数,它的第一个输入参数要求是返回残差平方和的自定义函数,不支持直接传入公式对象,与stat_smooth()的接口逻辑不匹配,才会触发"argument 'p' is missing"的报错。
  • 你前期拟合参数用的是nls函数,nls本身支持公式输入,完全适配stat_smooth()的调用规则,替换method参数即可解决问题。

修复后的代码修改点

你只需要将stat_smooth部分的代码替换为如下内容即可:

stat_smooth(method= "nls", 
            formula = y~a*x/(b+x),
            method.args = list(start = c(a = 3.8, b = 1457.2),
                               control = nls.control(maxiter = 100)),
            se=FALSE)

新增的nls.control参数用于放宽迭代次数,避免拟合收敛异常

补充说明

你之前使用的stat_regline_equation、stat_cor都是ggpubr提供的仅支持线性回归的工具函数,无法直接输出非线性拟合的方程与显著性指标,如果你需要在图上标注非线性拟合的a、b参数,可以先单独拟合nls模型提取参数,再用annotate()函数手动添加文本标注。

完整修正后代码

library(tidyverse)
library(ggpubr)
ggplot(data = STD, aes(x = Absorbance, y = STD)) +
  labs(title = "Quantifying PGD2 in cell culture lysates and its enzymatic reactions ",
       caption = "PGD2 ELISA")+
  geom_point(colour = "#69b3a2")+
  # 修正后的非线性拟合代码
  stat_smooth(method= "nls", 
              formula = y~a*x/(b+x),
              method.args = list(start = c(a = 3.8, b = 1457.2),
                                 control = nls.control(maxiter = 100)),
              se=FALSE)+
  xlab(expression(paste("%B/"~B[0])))+ 
  ylab(expression(paste("Prostaglandin"~ D[2], ~~ " MOX Concentration (pg/ml) ")))+
  
  theme(plot.background =  element_rect(fill = "transparent"),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black"))+
  
  theme(legend.spacing.y = unit(0.01, "cm"))+
  theme(legend.position = c(0.77, .91),
        legend.background = element_rect(colour = NA, fill = NA))+
  theme(plot.title = element_text(size = 12, face = "bold.italic"),
        plot.caption = element_text(hjust = 0))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 21:06:03