How to turn off the message "`geom_smooth()` using method = 'loess' and formula 'y ~ x'"

Viewed 5403

When I use ggplot and when I use stat_smooth() I get automatic messages like this:

geom_smooth() using method = 'loess' and formula 'y ~ x

The problem is I use RMarkdown to build a PDF and I want to show some plots there. This message will be shown there like this: enter image description here

How can I turn this message off? Or any way to not show it on the PDF with the plots.

3 Answers

If you replicate this example in RMarkdown, do you still get the error?

library(tidyverse)
set.seed(123)
df <- data.frame(group = as.factor(rep(1:3, each = 50)),
                 week = rep(1:50, 3),
                 rate = c(round(700 - rnorm(50, 100, 10) - 1:50 * 2, 0),
                          round(1000 - rnorm(50, 200, 10) - 1:50 * 2, 0),
                          round(1000 - rnorm(50, 200, 10) - 1:50 * 2, 0)))
df %>%
  ggplot(aes(x = week,
             y = rate,
             group = group,
             lty = group)) + 
  geom_line() +
  geom_point() +
  geom_smooth()

** This prints the warning/info **

Then try changing the final line:

library(tidyverse)
set.seed(123)
df <- data.frame(group = as.factor(rep(1:3, each = 50)),
                 week = rep(1:50, 3),
                 rate = c(round(700 - rnorm(50, 100, 10) - 1:50 * 2, 0),
                          round(1000 - rnorm(50, 200, 10) - 1:50 * 2, 0),
                          round(1000 - rnorm(50, 200, 10) - 1:50 * 2, 0)))
df %>%
  ggplot(aes(x = week,
             y = rate,
             group = group,
             lty = group)) + 
  geom_line() +
  geom_point() +
  geom_smooth(formula = y ~ x, method = "loess")

** No warning/info **

If rmarkdown is being used then making use of chunk options can be useful. So in your chunk set it like this:

{r warning=FALSE, message=FALSE}

You can turn off warnings and messages for the whole document by running this code in the first chunk:

```{r include=FALSE}
knitr::opts_chunk$set(warning=FALSE, message=FALSE)
```
Related