Hallo David Fair enough. Thanks for your explanation, which told me what should be done. It works perfectly for my example but I am still confused how to get expressions given to atop (or other functions) be evaluated and help page does not enlighten me, so I am still puzzled. When I borrow example from help, plot(1:10, type="n", xlab="", ylab="", main = "plot math & numbers") theta <- 1.23 ; mtext(bquote(hat(theta) == .(theta)), line= .25) for(i in 2:9) text(i, i+1, substitute(list(xi, eta) == group("(",list(x,y),")"), list(x = i, y = i+1))) #this is OK ex1 <- expression(" first: {f * minute}(x) " == {f * minute}(x)) ex2 <- expression(" second: {f * second}(x) " == {f * second}(x)) text(1, 9.6, ex1, adj=0) text(1, 9.0, ex2, adj=0) #and this is not text(2, 8, expression(atop(ex1, ex2))) text(2, 7, substitute( atop(ex1, ex2), list(ex1=ex1,ex2=ex2))) #and this works text(2, 6, expression(atop(1,2))) I tried to use eval when calling atop, but it did not work either. Therefore some hint in help page could be quite handy. Best regards Petr Pikal S pozdravem | Best Regards RNDr. Petr PIKAL Vedouc? V?zkumu a v?voje | Research Manager PRECHEZA a.s. n?b?. Dr. Edvarda Bene?e 1170/24 | 750 02 P?erov | Czech Republic Tel: +420 581 252 256 | GSM: +420 724 008 364 petr.pikal at precheza.cz | www.precheza.cz 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/ -----Original Message----- From: David Winsemius <dwinsemius at comcast.net> Sent: Thursday, April 7, 2022 12:04 AM To: PIKAL Petr <petr.pikal at precheza.cz>; Richard M. Heiberger <rmh at temple.edu> Cc: r-help at r-project.org Subject: Re: [R] [External] add equation and rsqared to plot On 4/6/22 14:36, PIKAL Petr wrote:> Hallo David > > Thanks for your answer. atop itself somehow cannot use expression made by substitute although those expressions itself are correct. I asked this question after roughly two hours of unsuccessful atempts. > > The second substitute or bquote solves the problem. Maybe this could propagate to help page, because although atop works smoothly with simple expressions, expressions with substitute are treated in a wrong way (at least by my opinion).It's not a problem with `atop`. It's a problem with your understanding of how R expressions and plotmath functions work. The argument(s) to `expression` are not evaluated. So `atop` was given two expressions `eq` and `req` and they in turn were not evaluated .... just taken as text values inside atop. Using either `substitute` (with a second argument list) or bquote (with its special dot function) forces evaluation by getting from the R symbol table those expressions which you had assigned as values of the symbols `eq` and `req`. The plotmath functions (not just atop but also `paste`, `frac`, `over` and all the rest on the ?plotmath pafge),? are designed to be "like" `expression` in not evaluating their arguments unless a "special value" like a Greek letter name or a defined %<op>% is found. Those functions are handled with a different parser than regular R functions. So they are not designed to go out to the R symbol table (which is where even locally defined object names are kept) to find values. Hope this helps; David. -- David.> > Thanks again. > > Best regards. > Petr > > > ________________________________________ > Od: David Winsemius <dwinsemius at comcast.net> > Odesl?no: 6. dubna 2022 23:20 > Komu: PIKAL Petr; Richard M. Heiberger > Kopie: r-help at r-project.org > P?edm?t: Re: [R] [External] add equation and rsqared to plot > > Try this: > > > 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), > r2adj = format(summary(m)$adj.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) > > } > req <- substitute(italic(r)^2~"="~r2* ","~~italic(adj.r)^2~"="~r2adj,l) > a_regs <- substitute( atop(eq, req), list(eq=eq,req=req)) } > > -- > > David. > > On 4/6/22 13:47, PIKAL Petr wrote: >> 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 at temple.edu> >> Odesl?no: 6. dubna 2022 22:36 >> Komu: PIKAL Petr >> Kopie: r-help at 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 at 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), >>> r2adj = format(summary(m)$adj.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) >>> >>> } >>> req <- substitute(italic(r)^2~"="~r2* ","~~italic(adj.r)^2~"="~r2adj,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. >>> >>> Please help. >>> >>> >>> 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%2Fww >>> w.precheza.cz%2Fzasady-ochrany-osobnich-udaju%2F&data=04%7C01%7C >>> rmh%40temple.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244 >>> 738e3110bd02ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZs >>> b3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0% >>> 3D%7C3000&sdata=MBtbONfsiMExWy0uorZMR1DglZindxErRMsooTztfUo%3D&a >>> mp;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%2Fww >>> w.precheza.cz%2Fen%2Fpersonal-data-protection-principles%2F&data >>> =04%7C01%7Crmh%40temple.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716 >>> e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown >>> %7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiL >>> CJXVCI6Mn0%3D%7C3000&sdata=2YDPoejYRlphL6h%2FWFmEEyR44KdmH2GDC9r >>> dLhFn4aU%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%2Fww >>> w.precheza.cz%2F01-dovetek%2F&data=04%7C01%7Crmh%40temple.edu%7C >>> 3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244738e3110bd02ccf6e5% >>> 7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjA >>> wMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%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%2Fww >>> w.precheza.cz%2Fen%2F01-disclaimer%2F&data=04%7C01%7Crmh%40templ >>> e.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244738e3110bd0 >>> 2ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZsb3d8eyJWIjo >>> iMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&a >>> mp;sdata=RCUE7DJbn54MvhWX4bqiNtPzoJLl7NK9tieMlFpe2rg%3D&reserved >>> =0 >>> >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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. >> 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/ >> >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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.
Yes, I also find it somewhat confusing. Perhaps this will help. I apologize beforehand if I have misunderstood and you already know all this. The key is to realize that plotmath works with **expressions**, unevaluated forms that include special plotmath keywords, like 'atop', and symbols. So... ## simple example with plotmath used in plot's title ## This will produce an error, as 'atop' is not an R function: plot(1,1, main = atop(x,y)) ## to make this work, we need an expression on the rhs of 'main =' . A simple way to do this is to use quote(): plot(1,1,main = quote(atop(x,y))) ## Note that this produce 'x' above 'y' **without quoting x and y**. That's because ## this is an expression that plotmath parses and evaluates according to its own rules, ## shown in ?plotmath ## Now suppose we have: x <- 'first line' y <- 'second line' ## and we want to display these quoted strings instead of 'x' and 'y' in the title ## Then this will *not* work -- it gives the same result as before: plot(1,1,main = quote(atop(x,y))) ## So what is needed here is R's 'computing on the language" capability to substitute ## the quoted strings for x and y in the expression. Here are two simple ways to do this: ## First using substitute() plot(1,1, main = substitute(atop(x,y), list (x =x, y = y))) ## Second, using bquote() plot(1,1, main = bquote(atop(.(x), .(y)))) ## More complicated expressions can be built up using plotmath's rules. ## But you need to be careful about distinguishing plotmath expressions and ## ordinary R expressions. For example: x <- pi/4 ## a number ## WRONG -- will display as written. bquote() is the same as quote() here. plot(1,1, main = bquote(sin(pi/4) == round(x,2))) ## WRONG -- will substitute value of x rounded to session default ## in previous. This is a mistake in using bquote plot(1,1, main = bquote(sin(pi/4) == round(.(x), 2))) ## RIGHT -- use of bquote plot(1,1, main = bquote(sin(pi/4) == .(round(x,2)))) ## or -- using substitute plot(1,1, main = substitute(sin(pi/4) == x, list(x = round(x,2)))) Hope this is helpful and, again, apologies if I have misunderstood. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Apr 8, 2022 at 7:42 AM PIKAL Petr <petr.pikal at precheza.cz> wrote:> > Hallo David > > Fair enough. Thanks for your explanation, which told me what should be done. It works perfectly for my example but I am still confused how to get expressions given to atop (or other functions) be evaluated and help page does not enlighten me, so I am still puzzled. > > When I borrow example from help, > > plot(1:10, type="n", xlab="", ylab="", main = "plot math & numbers") > theta <- 1.23 ; mtext(bquote(hat(theta) == .(theta)), line= .25) > for(i in 2:9) > text(i, i+1, substitute(list(xi, eta) == group("(",list(x,y),")"), > list(x = i, y = i+1))) > > #this is OK > ex1 <- expression(" first: {f * minute}(x) " == {f * minute}(x)) > ex2 <- expression(" second: {f * second}(x) " == {f * second}(x)) > text(1, 9.6, ex1, adj=0) > text(1, 9.0, ex2, adj=0) > > #and this is not > text(2, 8, expression(atop(ex1, ex2))) > text(2, 7, substitute( atop(ex1, ex2), list(ex1=ex1,ex2=ex2))) > > #and this works > text(2, 6, expression(atop(1,2))) > > I tried to use eval when calling atop, but it did not work either. Therefore some hint in help page could be quite handy. > > Best regards > Petr Pikal > > S pozdravem | Best Regards > RNDr. Petr PIKAL > Vedouc? V?zkumu a v?voje | Research Manager > PRECHEZA a.s. > n?b?. Dr. Edvarda Bene?e 1170/24 | 750 02 P?erov | Czech Republic > Tel: +420 581 252 256 | GSM: +420 724 008 364 > petr.pikal at precheza.cz | www.precheza.cz > > 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/ > > -----Original Message----- > From: David Winsemius <dwinsemius at comcast.net> > Sent: Thursday, April 7, 2022 12:04 AM > To: PIKAL Petr <petr.pikal at precheza.cz>; Richard M. Heiberger <rmh at temple.edu> > Cc: r-help at r-project.org > Subject: Re: [R] [External] add equation and rsqared to plot > > > On 4/6/22 14:36, PIKAL Petr wrote: > > Hallo David > > > > Thanks for your answer. atop itself somehow cannot use expression made by substitute although those expressions itself are correct. I asked this question after roughly two hours of unsuccessful atempts. > > > > The second substitute or bquote solves the problem. Maybe this could propagate to help page, because although atop works smoothly with simple expressions, expressions with substitute are treated in a wrong way (at least by my opinion). > > > It's not a problem with `atop`. It's a problem with your understanding of how R expressions and plotmath functions work. The argument(s) to `expression` are not evaluated. So `atop` was given two expressions `eq` and `req` and they in turn were not evaluated .... just taken as text values inside atop. Using either `substitute` (with a second argument > list) or bquote (with its special dot function) forces evaluation by getting from the R symbol table those expressions which you had assigned as values of the symbols `eq` and `req`. > > The plotmath functions (not just atop but also `paste`, `frac`, `over` and all the rest on the ?plotmath pafge), are designed to be "like" > `expression` in not evaluating their arguments unless a "special value" > like a Greek letter name or a defined %<op>% is found. Those functions are handled with a different parser than regular R functions. So they are not designed to go out to the R symbol table (which is where even locally defined object names are kept) to find values. > > > Hope this helps; > > David. > > > -- > > David. > > > > > Thanks again. > > > > Best regards. > > Petr > > > > > > ________________________________________ > > Od: David Winsemius <dwinsemius at comcast.net> > > Odesl?no: 6. dubna 2022 23:20 > > Komu: PIKAL Petr; Richard M. Heiberger > > Kopie: r-help at r-project.org > > P?edm?t: Re: [R] [External] add equation and rsqared to plot > > > > Try this: > > > > > > 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), > > r2adj = format(summary(m)$adj.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) > > > > } > > req <- substitute(italic(r)^2~"="~r2* ","~~italic(adj.r)^2~"="~r2adj,l) > > a_regs <- substitute( atop(eq, req), list(eq=eq,req=req)) } > > > > -- > > > > David. > > > > On 4/6/22 13:47, PIKAL Petr wrote: > >> 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 at temple.edu> > >> Odesl?no: 6. dubna 2022 22:36 > >> Komu: PIKAL Petr > >> Kopie: r-help at 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 at 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), > >>> r2adj = format(summary(m)$adj.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) > >>> > >>> } > >>> req <- substitute(italic(r)^2~"="~r2* ","~~italic(adj.r)^2~"="~r2adj,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. > >>> > >>> Please help. > >>> > >>> > >>> 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%2Fww > >>> w.precheza.cz%2Fzasady-ochrany-osobnich-udaju%2F&data=04%7C01%7C > >>> rmh%40temple.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244 > >>> 738e3110bd02ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZs > >>> b3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0% > >>> 3D%7C3000&sdata=MBtbONfsiMExWy0uorZMR1DglZindxErRMsooTztfUo%3D&a > >>> mp;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%2Fww > >>> w.precheza.cz%2Fen%2Fpersonal-data-protection-principles%2F&data > >>> =04%7C01%7Crmh%40temple.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716 > >>> e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown > >>> %7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiL > >>> CJXVCI6Mn0%3D%7C3000&sdata=2YDPoejYRlphL6h%2FWFmEEyR44KdmH2GDC9r > >>> dLhFn4aU%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%2Fww > >>> w.precheza.cz%2F01-dovetek%2F&data=04%7C01%7Crmh%40temple.edu%7C > >>> 3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244738e3110bd02ccf6e5% > >>> 7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjA > >>> wMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%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%2Fww > >>> w.precheza.cz%2Fen%2F01-disclaimer%2F&data=04%7C01%7Crmh%40templ > >>> e.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244738e3110bd0 > >>> 2ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZsb3d8eyJWIjo > >>> iMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&a > >>> mp;sdata=RCUE7DJbn54MvhWX4bqiNtPzoJLl7NK9tieMlFpe2rg%3D&reserved > >>> =0 > >>> > >>> > >>> [[alternative HTML version deleted]] > >>> > >>> ______________________________________________ > >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >>> 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. > >> 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/ > >> > >> > >> ______________________________________________ > >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >> 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. > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
#this is OK ex1 <- expression(" first: {f * minute}(x) " == {f * minute}(x)) ex2 <- expression(" second: {f * second}(x) " == {f * second}(x)) text(1, 9.6, ex1, adj=0) text(1, 9.0, ex2, adj=0) #and this is not text(2, 8, expression(atop(ex1, ex2))) text(2, 7, substitute( atop(ex1, ex2), list(ex1=ex1,ex2=ex2))) Plotmath does not like 'expression' objects inside of the expressions or calls that you pass it. You would be better off using quote() instead of expression(), or at least learning how they differ. plot(1:10) q1 <- quote(" first: {f * minute}(x) "=={f * minute}(x)) q2 <- quote("second: {f * second}(x) "=={f * second}(x)) text(1, 9.6, q1, adj=0) text(1, 9.0, q2, adj=0) # text(2, 8, expression(atop(q1, q2))) # quote() or expression(), this will not look up values of ex1 or ex2 text(6, 3, substitute( atop(q1, q2), list(q1=q1,q2=q2))) Looking at the output of substitute directly may help you understand the difference between quote and expression. expression(...) produces a list-like object containing any number of language objects and quote(...) produces a single language object.> e <- expression(x^2) > q <- quote(x^2) > substitute(sin(e), list(e=e))sin(expression(x^2))> substitute(sin(q), list(q=q))sin(x^2) -Bill On Fri, Apr 8, 2022 at 7:42 AM PIKAL Petr <petr.pikal at precheza.cz> wrote:> Hallo David > > Fair enough. Thanks for your explanation, which told me what should be > done. It works perfectly for my example but I am still confused how to get > expressions given to atop (or other functions) be evaluated and help page > does not enlighten me, so I am still puzzled. > > When I borrow example from help, > > plot(1:10, type="n", xlab="", ylab="", main = "plot math & numbers") > theta <- 1.23 ; mtext(bquote(hat(theta) == .(theta)), line= .25) > for(i in 2:9) > text(i, i+1, substitute(list(xi, eta) == group("(",list(x,y),")"), > list(x = i, y = i+1))) > > #this is OK > ex1 <- expression(" first: {f * minute}(x) " == {f * minute}(x)) > ex2 <- expression(" second: {f * second}(x) " == {f * > second}(x)) > text(1, 9.6, ex1, adj=0) > text(1, 9.0, ex2, adj=0) > > #and this is not > text(2, 8, expression(atop(ex1, ex2))) > text(2, 7, substitute( atop(ex1, ex2), list(ex1=ex1,ex2=ex2))) > > #and this works > text(2, 6, expression(atop(1,2))) > > I tried to use eval when calling atop, but it did not work either. > Therefore some hint in help page could be quite handy. > > Best regards > Petr Pikal > > S pozdravem | Best Regards > RNDr. Petr PIKAL > Vedouc? V?zkumu a v?voje | Research Manager > PRECHEZA a.s. > n?b?. Dr. Edvarda Bene?e 1170/24 | 750 02 P?erov | Czech Republic > Tel: +420 581 252 256 | GSM: +420 724 008 364 > petr.pikal at precheza.cz | www.precheza.cz > > 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/ > > -----Original Message----- > From: David Winsemius <dwinsemius at comcast.net> > Sent: Thursday, April 7, 2022 12:04 AM > To: PIKAL Petr <petr.pikal at precheza.cz>; Richard M. Heiberger < > rmh at temple.edu> > Cc: r-help at r-project.org > Subject: Re: [R] [External] add equation and rsqared to plot > > > On 4/6/22 14:36, PIKAL Petr wrote: > > Hallo David > > > > Thanks for your answer. atop itself somehow cannot use expression made > by substitute although those expressions itself are correct. I asked this > question after roughly two hours of unsuccessful atempts. > > > > The second substitute or bquote solves the problem. Maybe this could > propagate to help page, because although atop works smoothly with simple > expressions, expressions with substitute are treated in a wrong way (at > least by my opinion). > > > It's not a problem with `atop`. It's a problem with your understanding of > how R expressions and plotmath functions work. The argument(s) to > `expression` are not evaluated. So `atop` was given two expressions `eq` > and `req` and they in turn were not evaluated .... just taken as text > values inside atop. Using either `substitute` (with a second argument > list) or bquote (with its special dot function) forces evaluation by > getting from the R symbol table those expressions which you had assigned as > values of the symbols `eq` and `req`. > > The plotmath functions (not just atop but also `paste`, `frac`, `over` and > all the rest on the ?plotmath pafge), are designed to be "like" > `expression` in not evaluating their arguments unless a "special value" > like a Greek letter name or a defined %<op>% is found. Those functions are > handled with a different parser than regular R functions. So they are not > designed to go out to the R symbol table (which is where even locally > defined object names are kept) to find values. > > > Hope this helps; > > David. > > > -- > > David. > > > > > Thanks again. > > > > Best regards. > > Petr > > > > > > ________________________________________ > > Od: David Winsemius <dwinsemius at comcast.net> > > Odesl?no: 6. dubna 2022 23:20 > > Komu: PIKAL Petr; Richard M. Heiberger > > Kopie: r-help at r-project.org > > P?edm?t: Re: [R] [External] add equation and rsqared to plot > > > > Try this: > > > > > > 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), > > r2adj = format(summary(m)$adj.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) > > > > } > > req <- substitute(italic(r)^2~"="~r2* > ","~~italic(adj.r)^2~"="~r2adj,l) > > a_regs <- substitute( atop(eq, req), list(eq=eq,req=req)) } > > > > -- > > > > David. > > > > On 4/6/22 13:47, PIKAL Petr wrote: > >> 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 at temple.edu> > >> Odesl?no: 6. dubna 2022 22:36 > >> Komu: PIKAL Petr > >> Kopie: r-help at 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 at 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), > >>> r2adj = format(summary(m)$adj.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) > >>> > >>> } > >>> req <- substitute(italic(r)^2~"="~r2* > ","~~italic(adj.r)^2~"="~r2adj,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. > >>> > >>> Please help. > >>> > >>> > >>> 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%2Fww > >>> w.precheza.cz%2Fzasady-ochrany-osobnich-udaju%2F&data=04%7C01%7C > >>> rmh%40temple.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244 > >>> 738e3110bd02ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZs > >>> b3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0% > >>> 3D%7C3000&sdata=MBtbONfsiMExWy0uorZMR1DglZindxErRMsooTztfUo%3D&a > >>> mp;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%2Fww > >>> w.precheza.cz%2Fen%2Fpersonal-data-protection-principles%2F&data > >>> =04%7C01%7Crmh%40temple.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716 > >>> e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown > >>> %7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiL > >>> CJXVCI6Mn0%3D%7C3000&sdata=2YDPoejYRlphL6h%2FWFmEEyR44KdmH2GDC9r > >>> dLhFn4aU%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%2Fww > >>> w.precheza.cz%2F01-dovetek%2F&data=04%7C01%7Crmh%40temple.edu%7C > >>> 3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244738e3110bd02ccf6e5% > >>> 7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjA > >>> wMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%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%2Fww > >>> w.precheza.cz%2Fen%2F01-disclaimer%2F&data=04%7C01%7Crmh%40templ > >>> e.edu%7C3b28fb8737e146a9ee3208da1807da2e%7C716e81efb52244738e3110bd0 > >>> 2ccf6e5%7C0%7C0%7C637848719391803553%7CUnknown%7CTWFpbGZsb3d8eyJWIjo > >>> iMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&a > >>> mp;sdata=RCUE7DJbn54MvhWX4bqiNtPzoJLl7NK9tieMlFpe2rg%3D&reserved > >>> =0 > >>> > >>> > >>> [[alternative HTML version deleted]] > >>> > >>> ______________________________________________ > >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >>> 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. > >> 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/ > >> > >> > >> ______________________________________________ > >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >> 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. > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >[[alternative HTML version deleted]]