首页 > 解决方案 > 将 method.args 添加到 ggplot stat_smooth() 会导致错误

问题描述

我正在尝试数据科学简介中的这个例子:使用 R 的数据分析和预测算法,第 28 章

library(tidyverse)
library(dslabs)
data("polls_2008")

polls_2008 %>% ggplot(aes(day, margin)) +
  geom_point() + 
  geom_smooth(span = 0.15, method.args = list(degree=1))

这会导致错误“警告消息:计算失败stat_smooth():'what' 必须是函数或字符串。” 删除method.args...参数会导致正常操作。似乎将 method.args 定义为包括空列表在内的任何内容都会导致问题。

我正在使用为 Windows 和 ggplot2_3.3.2 构建的 R 版本 4.0.1 (2020-06-06)。谢谢你的帮助。

标签: rggplot2smoothing

解决方案


我仔细检查过,这有效。他们将方法的默认值从“auto”更改为 NULL。这仍然与没有 method.arg 的“自动”使用相同。但我认为您需要告诉它您正在运行什么方法,以便它可以正确使用 method.args

polls_2008 %>%
  ggplot(aes(day, margin)) +
  geom_point() + 
  geom_smooth(method = "loess", 
              span = 0.15, 
              method.args = list(degree=1))

推荐阅读