title: How do I plot the first derivative of the smoothing function?
body: <p>I have the following script that emulates the type of data structure I have and analysis that I want to do on it,</p>
  
  <pre><code>library(ggplot2)
  library(reshape2)
  
  n &lt;- 10
  df &lt;- data.frame(t=seq(n)*0.1, a  =sort(rnorm(n)), b  =sort(rnorm(n)),
                                 a.1=sort(rnorm(n)), b.1=sort(rnorm(n)), 
                                 a.2=sort(rnorm(n)), b.2=sort(rnorm(n)))
  head(df)
  
  mdf &lt;- melt(df, id=c('t'))
  ## head(mdf)
  
  levels(mdf$variable) &lt;- rep(c('a','b'),3)
  
  g &lt;- ggplot(mdf,aes(t,value,group=variable,colour=variable))
  g +
  stat_smooth(method='lm', formula = y ~ ns(x,3)) +
  geom_point() +
  facet_wrap(~variable) +
  opts()
  </code></pre>
  
  <p>What I would like to do in addition to this is plot the first derivative of the smoothing function against <code>t</code> and against the factors, <code>c('a','b')</code>, as well. Any suggestions how to go about this would be greatly appreciated.</p>