如何摆脱 geom_smooth 连续错误代码?

How do I get rid of geom_smooth continuous error code?

代码如下:

ggplot(exchangeGBP) + aes(Date, `GBP/EUR`) + geom_line(color = "deepskyblue") +
 geom_smooth(method = "lm")

错误代码:

`geom_smooth()` using formula 'y ~ x'

您没有看到错误。 R 具有三种不同类型的 条件 :错误、警告和消息。以下是 Hadley Wickham 在 Advanced R 中对它们的描述(一本了解更多 R 的好书):

Fatal errors are raised by stop() and force all execution to terminate. Errors are used when there is no way for a function to continue.

Warnings are generated by warning() and are used to display potential problems, such as when some elements of a vectorised input are invalid, like log(-1:2).

Messages are generated by message() and are used to give informative output in a way that can easily be suppressed by the user (?suppressMessages()). I often use messages to let the user know what value the function has chosen for an important missing argument.

Hadley 还编写了 ggplot2 包,他的最后一句话描述了正在发生的事情。您看到的是一条消息,告诉您函数选择的公式。

在我找到解决方案之前,让我们创建一个可重现的示例。

library(ggplot2)

ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_smooth()
#`geom_smooth()` using method = 'loess' and formula 'y ~ x'

有两种方法可以阻止消息出现。您可以按照书中摘录中的建议抑制消息的出现。但更好的方法是替换缺失值。在我的示例中,methodformula 都丢失了。您可以复制并粘贴消息中的值。

ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_smooth(method = 'loess', formula = 'y ~ x')

您的示例如下所示。

ggplot(exchangeGBP) + aes(Date, `GBP/EUR`) + geom_line(color = "deepskyblue") +
 geom_smooth(method = "lm", formula = 'y ~ x')

没有更多消息了![=​​17=]