4

I have a data-set, and I am trying to fit a linear model using lm(). That part is straight forward.

I can also plot this fit to a graph of the scatter plot using abline( lm( x ~ y ) ).

But now I want to write the parameters of the fit such as adjusted r-squared along the line.
So that if I hover plot different data-sets and their respective fits, I should be able to print some of the values for the fit along the line.

Is it possible to do this in R.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
Sam
  • 7,922
  • 16
  • 47
  • 62
  • 1
    Start [here](http://stackoverflow.com/q/7549694/324364) and then you'll just need to calculate the angle of the line and adjust the position you place the text at based on the some of the model's fitted values. – joran Dec 14 '12 at 16:18
  • @joran I looked at it, and getting to display the information as given in that graph is relatively straight forward. I am still thinking, but thought that collective wisdom might be better at solving this problem. – Sam Dec 14 '12 at 16:21
  • 3
    [Here is a very similar question](http://stackoverflow.com/questions/11767484/how-to-annotate-a-reference-line-at-the-same-angle-as-the-reference-line-itself/11767837#11767837) that received several good answers. – Josh O'Brien Dec 14 '12 at 16:27

1 Answers1

3

Yes, it's easy to do for straight lines, a curve is trickier, possibly simpler with monospaced fonts. The basic technique would be to just convert your linear equation such that you could derive a polar expression with an angle. Use that angle to set the angle for the text in the text command using the srt argument (from graphic parameters).

For a curve it would be a bit more tricky (perhaps simplified by non-proportional fonts). You need to work out vectors for each letter with an angle and then the width of each character (using strwidth). You'd generate a vector of angles and positions for your characters and loop through them calling text. You need a loop here because text cannot take a vector for the srt argument.

John
  • 23,360
  • 7
  • 57
  • 83