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

R语言使用ggplot2和reconplots绘图报seq.int的from需为有限数错误求助

问题排查与解决方案

报错原因

你的代码存在5个问题,共同触发了该报错:

  • 未加载reconplots包:curve_intersection()是该包的内置函数,未加载无法正常调用
  • 函数名调用错误:求两条曲线交点应该用reconplots的curve_intersection()函数,你代码里错写为base R的curve()绘图函数,根本没有计算出有效交点,导致curve_intersection$x为空非有限值,触发seq.int参数校验报错
  • stat_function语法错误:代码中stat_function(aes(x_range)),color=...)多写了一个右括号,参数没有正确传入函数
  • aes映射不规范:stat_function需要明确映射x轴变量,不能直接把x_range扔在aes()里
  • 线型参数拼写错误:linetype的虚线取值是dotted,不是你写的doted

修正后可运行完整代码

# 加载两个依赖包
library(ggplot2)
library(reconplots)

demanda <- function(q) (100 - q/10)
oferta <- function(q) (q/4)
x_range <- 1:500

# 计算曲线交点
curve_intersection <- curve_intersection(demanda, oferta, empirical = FALSE,
                                         domain = c(min(x_range), max(x_range)))

# 绘图
ggplot(data = data.frame(x = x_range), aes(x = x)) +
  stat_function(color = "green", size = 1, fun = demanda) +
  stat_function(color = "red", size = 1, fun = oferta) +
  geom_vline(xintercept = curve_intersection$x, linetype = "dotted") +
  geom_hline(yintercept = curve_intersection$y, linetype = "dotted") +
  theme_classic()

内容的提问来源于stack exchange,提问作者Renan .Pacheco

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 06:54:04