Quantcast

Display Equation on plot

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Display Equation on plot

Douglas M. Hultstrand
Hello,

I am using the lm to fit a linear model to data, I was wondering if
there is a way to display the equation on a plot using the extracted lm
coefficients?  I am using the plot() function to create the plot/.png.

Example:
lm_mod <- lm(data1~data2)

intercept = 48.54
slope = 0.4856

I want to display equation on a plot as y=0.4856x+48.54

Any thoughts or suggestions are appreciated.

Thanks,
Doug

--
---------------------------------
Douglas M. Hultstrand, MS
Senior Hydrometeorologist
Metstat, Inc. Windsor, Colorado
voice: 970.686.1253
email: [hidden email]
web: http://www.metstat.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Display Equation on plot

Uwe Ligges-3


Douglas M. Hultstrand wrote:

> Hello,
>
> I am using the lm to fit a linear model to data, I was wondering if
> there is a way to display the equation on a plot using the extracted lm
> coefficients?  I am using the plot() function to create the plot/.png.
>
> Example:
> lm_mod <- lm(data1~data2)
>
> intercept = 48.54
> slope = 0.4856
>
> I want to display equation on a plot as y=0.4856x+48.54
>
> Any thoughts or suggestions are appreciated.


See ?abline:
abline(lm_mod)

Uwe Ligges




> Thanks,
> Doug
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Display Equation on plot

Duncan Murdoch
In reply to this post by Douglas M. Hultstrand
On 3/24/2009 12:50 PM, Douglas M. Hultstrand wrote:

> Hello,
>
> I am using the lm to fit a linear model to data, I was wondering if
> there is a way to display the equation on a plot using the extracted lm
> coefficients?  I am using the plot() function to create the plot/.png.
>
> Example:
> lm_mod <- lm(data1~data2)
>
> intercept = 48.54
> slope = 0.4856
>
> I want to display equation on a plot as y=0.4856x+48.54

mtext(bquote( y == .(slope) * x + .(intercept)), side=1, line=4)

will put it in the bottom margin.  Use text() with appropriate
coordinates to put it in the plot area instead.

Duncan Murdoch

>
> Any thoughts or suggestions are appreciated.
>
> Thanks,
> Doug
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Display Equation on plot

Matthieu Dubois-3
In reply to this post by Douglas M. Hultstrand
Douglas M. Hultstrand <dmhultst <at> metstat.com> writes:

>
> Hello,
>
> I am using the lm to fit a linear model to data, I was wondering if
> there is a way to display the equation on a plot using the extracted lm
> coefficients?  I am using the plot() function to create the plot/.png.
>
> Example:
> lm_mod <- lm(data1~data2)
>
> intercept = 48.54
> slope = 0.4856
>
> I want to display equation on a plot as y=0.4856x+48.54

see ?bquote for building the equation expression
and ?mtext to display text within margins

e.g.
# generate random x and y values
x <- rnorm(100)
y <- 3*x+15 + rnorm(x)

lm_mod <- lm(y ~ x)

plot(x, y)
abline(lm_mod) # add regression line to the plot

lm_coef <- round(coef(lm_mod), 3) # extract coefficients
mtext(bquote(y == .(lm_coef[2])*x + .(lm_coef[1])),
adj=1, padj=0) # display equation

regards,

Matthieu

______________________________________________
[hidden email] 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.
Loading...