[R] How to write a function in a graph

Thomas Lumley tlumley at u.washington.edu
Tue Jul 18 23:35:13 CEST 2006


On Tue, 18 Jul 2006, junguo liu wrote:

> Dear R-ers,
>
>  I conducted a regression analysis, and then intended to add the 
> regression function (y=4.33+1.07x) in a graph. But the following code 
> can only give me a text like y=a+bx. Who can help me out? Thank you very 
> much in advance.

This is FAQ 7.13.

 	-thomas

>
>
>  CODE
>
>  # Read data
>  x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
>  y <- c(6, 5, 8, 9, 11, 10, 11, 12, 15)
>  data01 <- data.frame(x, y)
>
>  # Regression analysis
>  res.lm.y <- nls(y~a+b*x, start=list(a=1, b=2),data=data01)
>
>  # Obtain parameters
>  a<- coef(res.lm.y)["a"]
>  b<- coef(res.lm.y)["b"]
>  a
>  b
>  #a=4.33
>  #b=1.07
>
>  # Plot the results
>  def.par <- par()
>  par(mfrow=c(1,1),xaxs="i",yaxs="i")
>  plot(data01$x,data01$y,main="Fit",xlab="x",ylab="y")
>  lines(data01$x,predict(res.lm.y))
>
>  #=======
>  text (6, 13, expression(y==a+b*x))
>  #=======
>
>  ## I intended to add text like y=4.33+1.07x
>  ## but the above code added y=a+bx
>
>
>
> Swiss Federal Institute for Environmental Science and Technology (EAWAG)
> Ueberlandstrasse 133
> P.O.Box 611
> CH-8600 Duebendorf
> Switzerland
> Phone: 0041-18235012
> Fax: 0041-18235375
>
> ---------------------------------
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



More information about the R-help mailing list