title: Annotating the regresssion line equation and the R^2 value
body: <p>I have just started in <a href="/questions/tagged/r" class="post-tag" title="show questions tagged &#39;r&#39;" rel="tag">r</a> these past few weeks and this is my very first post.
  I have tried to implement a previous solution posted on <code>stackoverflow</code> without success. The link to the stackoverflow code is: 
  <a href="https://stackoverflow.com/questions/7549694/add-regression-line-equation-and-r2-on-graph">Add regression line equation and R^2 on graph</a></p>
  
  <p>I have a <a href="/questions/tagged/dataframe" class="post-tag" title="show questions tagged &#39;dataframe&#39;" rel="tag">dataframe</a> with an independent variable which is a timestamp in <a href="/questions/tagged/posixct" class="post-tag" title="show questions tagged &#39;posixct&#39;" rel="tag">posixct</a> format, starting in:
  '2011-05-27 16:00:02' and ending '2020-02-18 19:00:34'.
  When calling the linear model function <code>lm()</code> within the <a href="/questions/tagged/ggplot2" class="post-tag" title="show questions tagged &#39;ggplot2&#39;" rel="tag">ggplot2</a> function I am able to observe the correct trend line for the plot. However, I don't seem to be able to add the <a href="/questions/tagged/regresssion" class="post-tag" title="show questions tagged &#39;regresssion&#39;" rel="tag">regresssion</a> line equation and the <code>R^2</code> value to the plot.
  I think it may have something to do with the x-value in the <code>geom_text</code> function because this x-axis is in <a href="/questions/tagged/posixct" class="post-tag" title="show questions tagged &#39;posixct&#39;" rel="tag">posixct</a> format.</p>
  
  <p>An additional problem arises when calling the linear model function <code>lm()</code> on it's own. It doesn't give the correct y-intercept. I think this is because it is regressing back to 1970 because of the <a href="/questions/tagged/posixct" class="post-tag" title="show questions tagged &#39;posixct&#39;" rel="tag">posixct</a> format. Any help would be greatly appreciated.</p>
  
  <pre><code> ##Extracting hourly data from 5 minute data
  #
  data &lt;- filter(rawdata, grepl(":00:",timestamp)) %&gt;% droplevels
  #
  ##Covert timestamp factor to POSIXct
  newtimestamp &lt;- as.POSIXct(data$timestamp,format='%Y-%m-%d %H:%M:%S')
  #
  ##Stackoverflow code for adding regression line equation and R^2 value to plot
  lm_eqn &lt;- function(data){
    m &lt;- lm(demand ~ newtimestamp, data = data);
    eq &lt;- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
                     list(a = format(unname(coef(m)[1]), digits = 2),
                          b = format(unname(coef(m)[2]), digits = 2),
                          r2 = format(summary(m)$r.squared, digits = 3)))
    as.character(as.expression(eq));
  }
  #
  #
  ## Visualization - plotting total demand against time using ggplot2
  
  v1 &lt;- ggplot(data=data, aes(x=newtimestamp, y=demand)) +
    geom_point(col="steelblue", size=0.0005, show.legend = T) +
    geom_smooth(method="lm", col="red") +
    scale_x_datetime(breaks = date_breaks('1 year'),labels=date_format('%Y')) +
    ggtitle("Total Demand over Time") + 
    ylab("Demand") +
    xlab("Time") +
    ylim(c(10000, 60000))   # deletes outliers on the y-axis
  v1
  v2 &lt;- v1 + geom_text(x = '2011-05-27 16:00:02', y = 50000,label = lm_eqn(data), parse = TRUE)
  v2
  </code></pre>