首页 > 解决方案 > stats::model.frame 中的错误...:在 geom_smooth 中指定权重时找不到对象

问题描述

示例代码如下(在统计上可能没有意义,但问题是object not found)。而下一个作品:

mtcars %>% ggplot(aes(x = vs, y = mpg)) + geom_smooth(method="lm")

接下来不:

> mtcars %>% ggplot(aes(x = vs, y = mpg)) + geom_smooth(method=lm(weights = cyl))
Error in stats::model.frame(weights = cyl, drop.unused.levels = TRUE) : 
  object 'cyl' not found

为什么会出现这个错误?我能做些什么来解决它?

我也试过

> mtcars %>% ggplot(aes(x = vs, y = mpg)) + geom_smooth(method=lm(weights = "cyl"))
Error in terms.formula(formula, data = data) : 
  argument is not a valid model

和其他变体"", 里面有更多信息lm等,但没有成功。

非常感谢!

标签: rggplot2lmsmoothingweighted

解决方案


mtcars %>% ggplot(aes(x = vs, y = mpg)) +  
   stat_smooth(method="lm", aes(weight= cyl))

这将生成一个图表,但我不确定它是否是您所追求的。这里可能与您想使用weight的没有什么不同。weights


推荐阅读