On 4/2/2008 4:11 PM, Paul Johnson wrote:> I'm using R-2.6.2 on Fedora Linux 9.
>
> I've been experimenting with plotmath.
>
> I wish it were easier to combine expressions in plotmath with values
> from the R program itself. There are two parameters in the following
> example, the mean "mymean" and standard deviation
"mystd". I am able
> to use bquote to write elements into the graph title like
>
> mu = mymean
>
> and R will plot the symbol "mu" and the value of mymean from the
> program. But I want to combine that result in a string along with
> other results.
>
> Can I combine to result like this
>
> "Normal( mu = mymean , sigma = mystd)"
>
> Where symbols mu and sigma are replaced with Greek and mymean and
> mystd are drawn from program?
>
>
> ### 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 <pauljohn at ku.edu>
>
> mymean <- 0
> mystd <- 1.5
> myx <- seq( mymean - 3*mystd, mymean+ 3*mystd, length.out=500)
>
>
> myDensity <- dnorm(myx,mean=mymean,sd=mystd)
>
> ## This works
> plot(myx, myDensity, type="n", xlab="X",
ylab="Probability Density ",
> main=expression(mu == 0))
> ## This works
> plot(myx, myDensity, type="n", xlab="X",
ylab="Probability Density ",
> main=expression(sigma == 1.5))
>
> ## This works
> t1 <- bquote( mu== .(mymean))
>
> plot(myx, myDensity, type="n", xlab="X",
ylab="Probability Density ", main= t1 )
>
> ## This works
>
> t2 <- bquote( sigma== .(mystd))
>
> plot(myx, myDensity, type="n", xlab="X",
ylab="Probability Density ", main=t2)
>
> ## Can't figure how to combine t1 and t2 into plot title
>
> ### This fails!
> ### plot(myx, myDensity, type="n", xlab="X",
ylab="Probability Density
> ", main=paste( t1,t2) )
The reason it fails is that you're trying to use paste on expressions:
paste works on character strings. plotmath() uses paste formally in
expressions, but you're actually calling it. So you want to put
together an expression containing paste.
I'd do it by creating the whole title at once, e.g.
t1t2 <- bquote( paste(mu== .(mymean), ' ', sigma== .(mystd)) )
and use t1t2 as the title, but you can build it out of existing
expressions too, like this:
t1t2 <- bquote( paste( .(t1), ' ', .(t2) ) )
> plot(myx, myDensity, type="n", xlab="X",
ylab="Probability Density ", main= t1 )
>
> ## Can drop sigma in as margin text, though, on side 3.
> mtext(t2, 3)
> lines(myx,myDensity,lty=4, col=4) ### change line type & color if you
want
>
> ############################
>
> Supposing there is a way to do this, could I submit a working example
> to be added to the help page for plotmath ? How could I go about
> that?
First, put it together, then get agreement (in the R-devel list) from an
R core member that it would be a good addition. Then (depending on the
R core member), you might be asked to edit it into the plotmath man page
source
https://svn.r-project.org/R/trunk/src/library/grDevices/man/plotmath.Rd
or they might just put it in themselves.
Duncan Murdoch