Search code examples
rforecasting

How do I get rid of geom_smooth continuous error code?


Here is code:

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

Error Code:

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

Solution

  • You're not seeing an error. R has three different types of what are called conditions: errors, warnings, and messages. Here's how they're described in Advanced R by Hadley Wickham (an excellent book for learning more about 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 also wrote the ggplot2 package, and his last sentence describes what's happening. What you're seeing is a message that's telling you what formula the function picked.

    Before I get to the solutions, let's create a reproducible example.

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

    There are two ways to stop the message from appearing. You could suppress the message from appearing as suggested in the book excerpt. But the better approach is to replace the missing value. In my example, both method and formula are missing. You can copy and paste the values from the message.

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

    Your example would look like this.

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

    No more message!