[R] How to get the equation of a graph after i have plotted the datas?

Marc Schwartz marc_schwartz at comcast.net
Wed Feb 21 20:31:56 CET 2007

On Wed, 2007-02-21 at 18:20 +0000, ainhoaº lezama wrote:
> Hello!
> I have a doubt and i need a quick answer please!!
> I need to know if its possible to get in R the mathematical equation of a 
> graph that you have plotted. I mean i know the y and x values, but i want 
> the equation that relate them and that allow me to get the graph.
> This option is possible in Excel, but i have  too many datas and i cant 
> plotted them on it.
> If its possible in R, how?
> Thanks!!

Presuming that you are referring to a simple linear regression, using
some example data:

  # Create some random data
  # set the random seed first
  x <- rnorm(15)
  y <- x + rnorm(15)

  # Create a linear model
  LM <- lm(y ~ x)

  # Plot the data
  plot(x, y)

  # Draw the fitted line, passing the model
  # object as an argument

  # Now get the coefficients for the model
  > coef(LM)
  (Intercept)           x 
   0.05510098  1.08897520 

See ?abline, ?lm, ?coef and ?summary.lm for more information.

You would find value in reading "An Introduction to R", which is
available with your R installation or from the R web site under
"Manuals" as the above is covered there. Also the Posting Guide, for
which there is a link at the bottom of all e-mails coming from the R

If you need something a bit more, such as general curve fitting, using 

  RSiteSearch("curve fitting")

will get you many hits in the list archives and pointers to a variety of


Marc Schwartz

More information about the R-help mailing list