Skip to content
Back to formatted view

Raw Message

Message-ID: <E66794E69CFDE04D9A70842786030B933FA8F174@PA-MBX01.na.tibco.com>
Date: 2014-02-28T19:58:27Z
From: William Dunlap
Subject: do calculation within expression(), text in plot
In-Reply-To: <trinity-eb7f6903-14d4-4cc8-8164-d1b6e6fc5071-1393583217913@3capp-webde-bs18>

>    Unfortunately I can't get R to do the calculation for cor(nou,dak) when I
>    use expression()..
> 
>    text(0,2.2,expression(paste(r[Pearson],"=", cor(nou,dak) , sep= " ")),pos=4,
>    cex=1.5)

Use bquote() and its .() operator, as in
  with(mtcars, {
      plot(wt, mpg)
      text(4, 30, bquote(r[Pearson] ~ "=" ~ .(cor(wt, mpg))))
  })

Bill Dunlap
TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Tonja Krueger
> Sent: Friday, February 28, 2014 2:27 AM
> To: r-help at r-project.org
> Subject: [R] do calculation within expression(), text in plot
> 
> 
>    Dear Helpers,
>    I would like to add some text to my plot, containing a variable and the
>    calculated value for the variable.
>    As  I  would  like  to  produce  many  plots,  I hope this can be done
>    automatically.
>    Unfortunately I can't get R to do the calculation for cor(nou,dak) when I
>    use expression()..
> 
>    text(0,2.2,expression(paste(r[Pearson],"=", cor(nou,dak) , sep= " ")),pos=4,
>    cex=1.5)
> 
>    Is there a command that "stops" expression() so the result is calculated and
>    printed in the plot?
>    Thank you in advance,
>    Tonja
> ______________________________________________
> R-help at r-project.org 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.