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

R ggplot2绘制双误差线时rep的each参数报错行数不匹配问题

错误原因

rep函数的参数括号位置错误,each = length(test_error)被你写进了c()内部,R会将其识别为c()的第三个元素,最终生成的type列长度仅为3,同时原代码中iter列长度为10000,和拼接后长度为20000的Error列行数不匹配,因此触发报错。

修正后代码
# 修正数据框构造逻辑
error_df <- data.frame(
  iter = rep(seq_along(test_error), 2),
  Error = c(test_error, train_error),
  type = rep(c("test", "train"), each = length(test_error))
)
# 绘图时删除aes中不必要的each参数
ggplot(error_df, aes(iter, Error, color = type)) + geom_line()

运行上述代码即可正常输出训练、测试误差随迭代次数变化的折线图。

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

相关产品推荐
方舟 Agent Plan

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

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