99

I'm using geom_smooth() from ggplot2.

In Hadley Wickham's book ("ggplot2 - Elegant Graphics for Data Analysis") there is an example (page 51), where method="lm" is used. In the online manual there is no talk of the method argument. I see other Google results (and questions here) of people using method='loess'.

Is there a an exhaustive list somewhere that explains the options?

From what I can see, 'lm' draws a straight line, and 'loess' draws a very smooth curve. I assume there are others that draw more of a jagged line between reference points?

The se argument from the example also isn't in the help or online documentation.

FWIW here is my code.

p <- ggplot(output8, aes(age, myoutcome, group=id, colour=year_diag_cat2)) +
  geom_line() + scale_y_continuous(limits = c(lwr,upr))
p + geom_smooth(aes(group=year_diag_cat2), method="loess", size=2, se=F)
jbaums
  • 27,115
  • 5
  • 79
  • 119
nzcoops
  • 9,132
  • 8
  • 41
  • 52
  • 2
    Question and answers are outdated. Current documentation lists arguments: http://ggplot2.tidyverse.org/reference/geom_smooth.html – jan-glx Apr 13 '17 at 09:39
  • @StéphaneLaurent's link has moved (given it is 5+ years old now): https://stats.idre.ucla.edu/r/faq/how-can-i-explore-different-smooths-in-ggplot2/ – MokeEire Feb 12 '19 at 20:12

3 Answers3

42

Sometimes it's asking the question that makes the answer jump out. The methods and extra arguments are listed on the ggplot2 wiki stat_smooth page.

Which is alluded to on the geom_smooth() page with:

"See stat_smooth for examples of using built in model fitting if you need some more flexible, this example shows you how to plot the fits from any model of your choosing".

It's not the first time I've seen arguments in examples for ggplot graphs that aren't specifically in the function. It does make it tough to work out the scope of each function, or maybe I am yet to stumble upon a magic explicit list that says what will and will not work within each function.

Riccardo
  • 111
  • 7
nzcoops
  • 9,132
  • 8
  • 41
  • 52
  • 4
    yeah, the documentation is a little bit of a mess. I don't like how Springer charges over 40 bucks for hadley's book, which contains the information lacking from the website. – wespiserA Aug 10 '11 at 05:49
  • @wespiserA in what sense is the information missing from the website (or package help)? nzcoops was looking in the wrong place. That is not to say that the help for `geom_smooth()` couldn't be improved, say by making explicit mention of `method` and to see `stat_smooth()` - at moment this only happens in a comment in the examples - and a polite email to Hadley would, I am certain, work wonders. Documentation writing is hard, especially when you are so close to the code; as the developer, you are no longer a user and will find it hard to write documentation for users. – Gavin Simpson Aug 10 '11 at 08:45
  • 1
    @nzcoops the reason it isn't documented as an argument to `geom_smooth()` is that that function doesn't have or take that argument. The geom code is for producing objects that, when printed, do the drawing. The convention in ggplot is to have `stat_foo()` functions, even to the extent of having `stat_identity()` when you don't want to compute anything. That said, Hadley welcomes comment/critique of his code/packages so drop him and email and suggest higher profile links between `geom_` documentation and their associated `stat_` – Gavin Simpson Aug 10 '11 at 08:50
  • 2
    @Gavin, yup I've emailed Hadley a few times in the past, thought the question was still valuable here given I've no doubt others will google it at some point. Your comment about 'doesn't have or take that argument' is incorrect though. It does. It may well pass it onto stat_smooth() but it does 'take' it as the examples show. I also didn't want to bug Hadley over something I knew someone could answer quickly, albeit myself :/ – nzcoops Aug 11 '11 at 00:12
22

The method argument specifies the parameter of the smooth statistic. You can see stat_smooth for the list of all possible arguments to the method argument.

Tony Cronin
  • 1,623
  • 1
  • 24
  • 30
wespiserA
  • 3,131
  • 5
  • 28
  • 36
2

The se argument from the example also isn't in the help or online documentation.

When 'se' in geom_smooth is set 'FALSE', the error shading region is not visible