Plotmath with values?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Plotmath with values?

Mike Prager
I hope to use the plotmath facility to print titles that mix
math and values of R variables.

The help for "plotmath" has an example, which after repeated
reading, I find baffling. Likewise, I have read the help file
for "substitute" (wqhich seems to be needed) without ever
understanding what it does, other than being used in some magic
incantations.

I would like to do something like this:

     dev.new()
     aa <- round(pi,2)
     plot(1:3, 1:3, main = ~ a == aa)

and have the main title read "a = 3.14"

but of course it reads "a = aa".

>From a user's point of view -- one who has never written a
parser nor taken a course in compilers -- what is needed is the
nonexistent function "value" usable in plotmath expressions to
produce the value of its argument, as

     plot(1:3, 1:3, main = ~ a == value(aa))

How can this be done?

THANKS!

--
Mike Prager, NOAA, Beaufort, NC
* Opinions expressed are personal and not represented otherwise.
* Any use of tradenames does not constitute a NOAA endorsement.

______________________________________________
[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
|

Re: Plotmath with values?

Gabor Grothendieck
Try:

aa <- 22
plot(1, main = bquote(a == .(aa)))


On Wed, Dec 31, 2008 at 11:47 AM, Mike Prager <[hidden email]> wrote:

> I hope to use the plotmath facility to print titles that mix
> math and values of R variables.
>
> The help for "plotmath" has an example, which after repeated
> reading, I find baffling. Likewise, I have read the help file
> for "substitute" (wqhich seems to be needed) without ever
> understanding what it does, other than being used in some magic
> incantations.
>
> I would like to do something like this:
>
>     dev.new()
>     aa <- round(pi,2)
>     plot(1:3, 1:3, main = ~ a == aa)
>
> and have the main title read "a = 3.14"
>
> but of course it reads "a = aa".
>
> >From a user's point of view -- one who has never written a
> parser nor taken a course in compilers -- what is needed is the
> nonexistent function "value" usable in plotmath expressions to
> produce the value of its argument, as
>
>     plot(1:3, 1:3, main = ~ a == value(aa))
>
> How can this be done?
>
> THANKS!
>
> --
> Mike Prager, NOAA, Beaufort, NC
> * Opinions expressed are personal and not represented otherwise.
> * Any use of tradenames does not constitute a NOAA endorsement.
>
> ______________________________________________
> [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.
>

______________________________________________
[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
|

Re: Plotmath with values?

cberry
In reply to this post by Mike Prager
On Wed, 31 Dec 2008, Mike Prager wrote:

> I hope to use the plotmath facility to print titles that mix
> math and values of R variables.
>
> The help for "plotmath" has an example, which after repeated
> reading, I find baffling. Likewise, I have read the help file
> for "substitute" (wqhich seems to be needed) without ever
> understanding what it does, other than being used in some magic
> incantations.
>
> I would like to do something like this:
>
>     dev.new()
>     aa <- round(pi,2)
>     plot(1:3, 1:3, main = ~ a == aa)


It has another example, too, along these lines:

  plot(1:3, 1:3, main = bquote(a == .(aa)))


You have to follow a couple of 'See Also' links from the plothmath page to
get to

  ?bquote

HTH,

Chuck


>
> and have the main title read "a = 3.14"
>
> but of course it reads "a = aa".
>
>> From a user's point of view -- one who has never written a
> parser nor taken a course in compilers -- what is needed is the
> nonexistent function "value" usable in plotmath expressions to
> produce the value of its argument, as
>
>     plot(1:3, 1:3, main = ~ a == value(aa))
>
> How can this be done?
>
> THANKS!
>
> --
> Mike Prager, NOAA, Beaufort, NC
> * Opinions expressed are personal and not represented otherwise.
> * Any use of tradenames does not constitute a NOAA endorsement.
>
> ______________________________________________
> [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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:[hidden email]            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

______________________________________________
[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
|

Re: Plotmath with values?

Mike Prager
"Charles C. Berry" <[hidden email]> wrote:

> On Wed, 31 Dec 2008, Mike Prager wrote:
>
> > I hope to use the plotmath facility to print titles that mix
> > math and values of R variables.
[...]

>
>   plot(1:3, 1:3, main = bquote(a == .(aa)))
>
>
> You have to follow a couple of 'See Also' links from the plothmath page to
> get to
>
>   ?bquote
>
> HTH,

Chuck, THANKS!  It helps very much.  --Mike

______________________________________________
[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
|

Re: Plotmath with values?

Sarah Goslee
In reply to this post by Gabor Grothendieck
Hi all,

I hadn't used bquote before, so I gave this a try, only to encounter some odd
behavior.

R 2.8.0 on Fedora Core 8 (no RPM for 2.8.1 yet)
"vanilla" session with no packages loaded (same behavior with my usual
set of packages).

On Wed, Dec 31, 2008 at 12:30 PM, Gabor Grothendieck
<[hidden email]> wrote:
> Try:
>
> aa <- 22
> plot(1, main = bquote(a == .(aa)))
>

R --vanilla

aa <- 22
x11()
plot(1, main=paste("a == ", aa)) # plot with title
plot(, main = bquote(a == .(aa))) # plot _without_ title
plot(1, main=paste("a == ", aa)) # plot _without_ title - ???

plot(1, main="abc") # plot _without_ title - ???
dev.off()

# after trying bquote(), no plots have titles until I close
# the device and open a new one.

x11()
plot(1, main="abc") # plot with title
dev.off()

The png device does not have a plot with a title. postscript() works as
expected.

What am I missing?

Thanks,
Sarah
--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[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
|

Re: Plotmath with values?

Sarah Goslee
Sorry, apparently I can't type. Gabor's example and my test case
should be identical, and weren't in my original email (now fixed).

Sarah

On Wed, Dec 31, 2008 at 1:42 PM, Sarah Goslee <[hidden email]> wrote:

> Hi all,
>
> I hadn't used bquote before, so I gave this a try, only to encounter some odd
> behavior.
>
> R 2.8.0 on Fedora Core 8 (no RPM for 2.8.1 yet)
> "vanilla" session with no packages loaded (same behavior with my usual
> set of packages).
>
> On Wed, Dec 31, 2008 at 12:30 PM, Gabor Grothendieck
> <[hidden email]> wrote:
>> Try:
>>
>> aa <- 22
>> plot(1, main = bquote(a == .(aa)))
>>
>
> R --vanilla
>
> aa <- 22
> x11()
> plot(1, main=paste("a == ", aa)) # plot with title
> plot(1, main = bquote(a == .(aa))) # plot _without_ title
> plot(1, main=paste("a == ", aa)) # plot _without_ title - ???
>
> plot(1, main="abc") # plot _without_ title - ???
> dev.off()
>
> # after trying bquote(), no plots have titles until I close
> # the device and open a new one.
>
> x11()
> plot(1, main="abc") # plot with title
> dev.off()
>
> The png device does not have a plot with a title. postscript() works as
> expected.
>
> What am I missing?



--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[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
|

Re: Plotmath with values?

PaulJohnson32gmail
In reply to this post by Mike Prager
On Wed, Dec 31, 2008 at 10:47 AM, Mike Prager <[hidden email]> wrote:

> I hope to use the plotmath facility to print titles that mix
> math and values of R variables.
>
> The help for "plotmath" has an example, which after repeated
> reading, I find baffling. Likewise, I have read the help file
> for "substitute" (wqhich seems to be needed) without ever
> understanding what it does, other than being used in some magic
> incantations.
>
> I would like to do something like this:
>
>     dev.new()
>     aa <- round(pi,2)
>     plot(1:3, 1:3, main = ~ a == aa)
>
> and have the main title read "a = 3.14"
>
> but of course it reads "a = aa".
>

I agree this is a very difficult part of using R.  I asked this exact
same kind of question last year.  If you go to an R-help email archive
for April 2, 2008

"Trouble combining plotmath, bquote, expressions"

you will find some discussion in the list.

For a while, I got pretty excited and started working on better
example code to put into the R distribution, but lost initiative when
I saw the magnitude of the problem.  This example code will show
several usages of bquote and an alternative substitute approach.  The
result seems not to look so beautiful as I recall, but isn't that
always the way it goes :).


### Filename: Normal1_2008.R
### Paul Johnson March 31, 2008
### This code should be available somewhere in
http://pj.freefaculty.org.  If it is not
### email me <[hidden email]>



mu <- 10.034

sigma <- 12.5786786

myx <- seq( mu - 3.5*sigma,  mu+ 3.5*sigma, length.out=500)

myDensity <- dnorm(myx,mean=mu,sd=sigma)


# Here's one way to retrieve the values of mu and sigma and insert
them into an expression
t1t2 <- bquote (paste("Normal(", mu== .(round(mu,2)), ',', sigma==
.(round(sigma,2)),")") )

plot(myx, myDensity, type="l", xlab="x", ylab="Probability Density ", main=t1t2)


t1t2 <- bquote (paste("Normal (   ", mu== .(round(mu,2)), '  ,  ',
sigma^2== .(round(sigma^2,2))," )") )
### Note spaces manually inserted above are needed, otherwise plotmath
overlaps "l" of normal with first parenthesis
plot(myx, myDensity, type="l", xlab="x", ylab="Probability Density ", main=t1t2)




### Had difficulty finding syntax for substitute to combine symbols mu
and sigma with values.
### Following is best I can figure, no simpler or obvious than previous method.
##t1 <- substitute ( mu == a ,  list (a = mu))
##t2 <- substitute ( sigma == a, list(a = sigma))
##t1t2 <- bquote(paste("Normal(", .(t1),",", .(t2),")" ) )



t1t2 <-  substitute( "Normal" ~~ group( "(", list(mu==mu1,
sigma^2==sigma2), ")") ,  list(mu1=round(mu,2),
sigma2=round(sigma^2,2)))


plot(myx, myDensity, type="l", xlab="x", ylab="Probability Density ", main=t1t2)



plot(myx, myDensity, type="l", xlab="x", ylab="Probability Density ",
main=t1t2, axes=F)
axis(2, pos= mu - 3.6*sigma)
axis(1, pos=0)


# bquote creates an expression that text plotters can use
t1 <-  bquote( mu== .(mu))

text( mu, 0.00, t1, pos=3)

ss = 0.2 * max(myDensity)

arrows( x0=mu, y0= ss, x1=mu+sigma, y1=ss, code=3, angle=90, length=0.1)

t2 <-  bquote( sigma== .(round(sigma,2)))

text( mu+0.5*sigma, 1.15*ss, t2)


normalFormula <- expression (f(x) == frac (1, sqrt(2*pi)*sigma) *
e^{~~ - ~~ frac(1,2)~~ bgroup("(", frac(x-mu,sigma),")")^2})

text ( mu + 0.5*sigma, max(myDensity)- 0.10 * max(myDensity),
normalFormula, pos=4)

### Theory says we should have about 2.5% of the area to the left of:
-1.96 * std.dev

criticalValue <- mu -1.96 * sigma
specialX <-  myx[myx <= criticalValue]

### mark the critical value in the graph

text ( criticalValue, 0 , label= paste(round(criticalValue,2)), pos=1)

specialY <- myDensity[myx < criticalValue]

polygon(c(specialX[1], specialX, specialX[length(specialX )]), c(0,
specialY, 0), density=c(-110),col="lightgray" )

shadedArea <- round(pnorm(mu - 1.96 * sigma, mean=mu, sd=sigma),4)

### Hard to position this text "just right"
al <- paste( "Prob(", "x <=" , round(criticalValue,2),")\n=","F(",
round(criticalValue,2) ,")\n=", round(shadedArea,3),sep="")
text(  criticalValue- sigma, myDensity[length(specialX)], labels=al, pos=3)

ss <- 0.1 * max(myDensity)
arrows( x0=mu, y0= ss, x1=mu-1.96*sigma, y1=ss, code=3, angle=90, length=0.1)

text( mu - 2.0*sigma, 1.15*ss,
bquote(paste(.(round(criticalValue,2)),phantom(1) == mu - 1.96," ",
sigma,sep=" ")),pos=4 )





> >From a user's point of view -- one who has never written a
> parser nor taken a course in compilers -- what is needed is the
> nonexistent function "value" usable in plotmath expressions to
> produce the value of its argument, as
>
>     plot(1:3, 1:3, main = ~ a == value(aa))
>
> How can this be done?
>
> THANKS!
>
> --
> Mike Prager, NOAA, Beaufort, NC
> * Opinions expressed are personal and not represented otherwise.
> * Any use of tradenames does not constitute a NOAA endorsement.
>
> ______________________________________________
> [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.
>



--
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas

______________________________________________
[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.