# [R] [External] add equation and rsqared to plot

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Wed Apr 6 22:47:19 CEST 2022

```Hallo Richard.

Did you try the example? I Used atop but with the syntax I made, the result is

eq
req

but not the equations.

I send the picture, but I am not sure if it will go through.

Best regards
Petr

________________________________________
Od: Richard M. Heiberger <rmh using temple.edu>
Odesláno: 6. dubna 2022 22:36
Komu: PIKAL Petr
Kopie: r-help using r-project.org
Předmět: Re: [External] [R] add equation and rsqared to plot

I think you are looking for atop(a,b)
See ? plotmath

> On Apr 06, 2022, at 15:58, PIKAL Petr <petr.pikal using precheza.cz> wrote:
>
> Dear all
>
>
> I want to add equation and rsquared values to plot and I am lost in correct expression form. I want to have 2 lines, one with equation and one with r squared values.
>
>
> Here is what I made.
>
> # function to extract values from lm fit.
>
>
> lm_eqn = function(m) {
>  l <- list(a = format(coef(m)[1], digits = 4),
>      b = format(abs(coef(m)[2]), digits = 4),
>      r2 = format(summary(m)\$r.squared, digits = 3),
>  if (coef(m)[2] >= 0)  {
>    eq <- substitute(italic(y) == a + b %.% italic(x), l)
>  } else {
>    eq <- substitute(italic(y) == a - b %.% italic(x),l)
>
>  }
>  expression(atop(eq, req))
> }
>
>
> #Example
>
> x <- 1:10
> y <- x*5 +rnorm(10)
> plot(x,y)
> fit <- lm(y~x)
> text(4,40, lm_eqn(fit))
>
>
> I know that both eq and req are correct expressions and when the last line in function is either eq or req, the example gives correct result.
>
>
> But how to get both expressions one above the other is mystery.
>
>
>
> Best regards.
>
> Petr
>
>
>
> Osobn� �daje: Informace o zpracov�n� a ochran� osobn�ch �daj� obchodn�ch partner� PRECHEZA a.s. jsou zve�ejn�ny na: https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.precheza.cz%2Fzasady-ochrany-osobnich-udaju%2F&data=04%7C01%7Crmh%40temple.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=MBtbONfsiMExWy0uorZMR1DglZindxErRMsooTztfUo%3D&reserved=0 | Information about processing and protection of business partner's personal data are available on website: https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.precheza.cz%2Fen%2Fpersonal-data-protection-principles%2F&data=04%7C01%7Crmh%40temple.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=2YDPoejYRlphL6h%2FWFmEEyR44KdmH2GDC9rdLhFn4aU%3D&reserved=0
> D�v�rnost: Tento e-mail a jak�koliv k n�mu p�ipojen� dokumenty jsou d�v�rn� a podl�haj� tomuto pr�vn� z�vazn�mu prohl�en� o vylou�en� odpov�dnosti: https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.precheza.cz%2F01-dovetek%2F&data=04%7C01%7Crmh%40temple.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=z7TNZSbfNXlB%2FZt0ekl8P1kTu1l0eBFVoLvewSSdQDg%3D&reserved=0 | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.precheza.cz%2Fen%2F01-disclaimer%2F&data=04%7C01%7Crmh%40temple.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=RCUE7DJbn54MvhWX4bqiNtPzoJLl7NK9tieMlFpe2rg%3D&reserved=0
>
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.

Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.pdf
Type: application/pdf
Size: 1788 bytes
Desc: test.pdf
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20220406/29a3dc98/attachment.pdf>
```