4

I have a package that has a bunch of functions that generate ggplot2 objects. Recently, ggplot2 added an update that gives a message that says:

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

I know why ggplot2 is saying that, but I don't need to hear it every time I run a plot (and it confuses my users because they think they did something wrong). I know I can suppress the messages by wrapping a print statement in suppressMessages but I don't want to print a plot, I want to return it. If I print it, it will display the plot, even when I don't want to display it.

Any ideas? Here's a minimum working example.

f = function(y,x,data){
    p = ggplot(data, aes_string(x,y)) + geom_point() + geom_smooth(se=F)
    #suppressMessages(return(p))    ### doesn't work
    suppressMessages(print(p))      ### works, but I don't want to PRINT it
}
data(iris)
head(iris)
f("Sepal.Length", "Sepal.Width", iris)
dfife
  • 348
  • 1
  • 12
  • Maybe set `method = 'loess'` instead of the default? – JBGruber Oct 04 '19 at 13:32
  • the message just changes to something shorter. – dfife Oct 04 '19 at 13:47
  • The message is generated only when the plot is plotted, so you could wrap `suppressMessages` around your call to `f`. Notably - the warning won't appear if you assign the result of `f` (i.e., it's not implicitly printing) – iod Oct 04 '19 at 13:58
  • Sorry, the message is still there, even with method="loess" when se=F. – dfife Oct 04 '19 at 17:00

2 Answers2

5

You could just set method = 'loess' instead of method = 'auto', which is the default:

library(ggplot2)
f = function(y,x,data){
  p = ggplot(data, aes_string(x,y)) + geom_point() + geom_smooth(method = "loess")
  return(p)
}

data(iris)

gg <- f("Sepal.Length", "Sepal.Width", iris)
gg

Created on 2019-10-04 by the reprex package (v0.3.0)

I don't see a message here, not even a short one.

The other option is to define a custom print function and give your output object a different class:

f = function(y,x,data){
  p = ggplot(data, aes_string(x,y)) + geom_point() + geom_smooth()
  class(p) <- c("gg_silent", class(p))
  return(p)
}

print.gg_silent <- function(gg) {
  suppressMessages(ggplot2:::print.ggplot(gg))
}

This will suppress the messages when the returned object is printed. Since this adds a class rather than overwrite the old one, you can still add arguments with + without a problem. Still I would say that the first option should be the better one.

JBGruber
  • 11,727
  • 1
  • 23
  • 45
  • That new method is a brilliant solution! – iod Oct 04 '19 at 13:59
  • Maybe it depends on the version of ggplot2, but current ggplot2 still prints a message: "`geom_smooth()` using formula 'y ~ x'". If you don't want to have this message, you need to pass the argument `fromula = y ~ x`. – jarauh Sep 10 '21 at 12:32
1

According to the source code, stat_smooth and geom_smooth will print messages unless both method and formula are given as arguments (furthermore, method should not be "auto"). If you use method = "loess", then most likely you want formula = y ~ x. So the following should work silently:

f = function(y,x,data){
    ggplot(data, aes_string(x,y)) + geom_point() +
        geom_smooth(se=F, method = "loess", formula = y ~ x)
}
data(iris)
head(iris)
f("Sepal.Length", "Sepal.Width", iris)

(I also removed the extra assignment and the explicit return statements, but that's a matter of taste.)

jarauh
  • 1,836
  • 22
  • 30