hadley/ggplot2 — pull request #369 by wch (2012-01-23 05:22:58+00:00)
Title: Fix stat_smooth auto-method bug (#367)

It now chooses auto method based on the size of the largest group, and displays a message about which method is being used.

The diff looks really ugly and confusing, but the change was pretty simple. I basically moved the method-decision block of code from $calculate to $calculate_groups, added messages, and updated the $calculate_groups function to handle the parameters correctly. But the diff viewer thinks I deleted and inserted lines of code all over the place. If the diff is too confusing, I might be able to redo it and make some trivial changes (spacing, etc) that result in a clearer diff.

In these examples, the smooth lines are now directly on top of each other, as they should be.

```R
library(mgcv)

set.seed(125)
dat <- data.frame(xvar = runif(1000), yvar = rnorm(1000))
# Make two copies of the same values, in group A and B
dat <- rbind(cbind(dat, group='A'), cbind(dat, group='B'))

# Drop one of the B's, so there are 1000 A's and 999 B's
dat1 <- dat[-2000,]
# Drop one of the A's, so both A and B have the same 999 points
dat2 <- dat1[-1000,]

# Since there are >=1000 points in one group, this uses gam.
# The smoothing lines are now almost identical since the only difference
# between A and B is one data point, which you can see near (.5, 0)
ggplot(dat1, aes(x=xvar, y=yvar, colour=group)) +
geom_point(alpha=.5) + geom_smooth(alpha=.5)
# geom_smooth: method="auto" and size of largest group is >=1000, so using gam with a cubic regression basis.

# Since there are <1000 points in both groups, this uses loess
# The smoothing lines look identical.
# For this data, the loess fit looks straighter than gam used for dat1
ggplot(dat2, aes(x=xvar, y=yvar, colour=group)) +
geom_point(alpha=.5) + geom_smooth(alpha=.5)
# geom_smooth: method="auto" and size of largest group is <1000, so using loess smoothing.

```