title: Make geom_smooth() from ggplot2 adapt to number of observations
body: <p>How can I make the <code>method</code> argument of <code>geom_smooth()</code> from <code>ggplot</code> be dynamic and adapt to the number of data points in a group?</p>
  
  <p>For example, I have data in the following format:</p>
  
  <pre><code>1. DATE PRODUCT SIZE
  2. 3/1/2017 A 10
  3. 3/2/2017 B 14
  4. 3/3/2017 C 25
  5. 3/4/2017 A 16
  6. etc.
  </code></pre>
  
  <p>This charts completely fine and adds a loess fit to each group (<code>PRODUCT</code>) with the following code (each <code>PRODUCT</code> group has about 20 entries):</p>
  
  <pre><code>DT&lt;-read.csv("TEST_DATA.csv")
  
  DT$DATE&lt;-as.Date(DT$DATE, "%m/%d/%Y")
  
  myPlot&lt;-ggplot(DT, aes(DATE, SIZE, color = PRODUCT))
  
  myPlot + geom_point() + geom_smooth(method = "loess", se = FALSE)
  </code></pre>
  
  <p>However, let's say I add in just 2 data points for a 4th Product "<code>D</code>". I then get the following warning messages and no loess fit lines are added to the plot for ANY group.</p>
  
  <pre><code>Warning messages:
  1: In simpleLoess(y, x, w, span, degree = degree, parametric = parametric,  ... : span too small.   fewer data values than degrees of freedom.
  </code></pre>
  
  <p>I believe this warning is due to the fact that the number of observations for product <code>D</code> is less that the degrees of freedom for the loess fit.</p>
  
  <p>Setting <code>method = "auto"</code>, chooses <code>"loess"</code> anyway so that doesn't help and setting method to <code>"lm"</code> is not what I want.</p>
  
  <p>I would like to do the following but can't quite get it to work and am wondering if someone can help?</p>
  
  <pre><code>myPlot + geom_point() + geom_smooth(data = DT, method = if(length(DT$PRODUCT)&lt;5) {"lm"} else {"loess"}, se = F)
  </code></pre>
  
  <p>As you can see, I am trying to have <code>geom_smooth()</code> use <code>method = "lm"</code> if any groups have less than 5 observations, otherwise use the <code>"loess"</code> method. But I can't quite figure out how to access the number of observations of each group within the <code>geom_smooth()</code> function.</p>