On 07/07/10 06:03, Paul Johnson wrote:
> [...]
> Hi, Duncan and David
>
> Thanks for looking.  I suspect from the comment you did not run the
> code.  The expression examples I give do work fine already.  But I
> have to explicitly put in values like 1.96 to make them work.  I'm
> trying to avid that with substitute, which does work for b2, b3, b4,
> b5, all but b1.  Why just one?
>
> I'm uploading a picture of it so you can see for yourself:
>
> http://pj.freefaculty.org/R/plotmathwrong.pdf
>
> please look in the middle axis.
>
> Why does only b1 not work, but the rest do?
>
>    

Because you only had one (as.)expression in your original code, 
perhaps?  This version works for me:

### Filename: plotMathProblem.R
### Paul Johnson July 5, 2010
### email me<paulj...@ku.edu>
### 2010-07-06 AE : Changes.

   sigma<- 10.0
   mu<- 4.0

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

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

   ### xpd needed to allow writing outside strict box of graph
   ### Need big bottom margin to add several x axes
   par(xpd=TRUE, ps=10, mar=c(18,2,2,2))

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

   lines(c(myx[1],myx[length(myx)]),c(0,0)) ### closes off axes


   addInteriorLine<- function(x, m, sd){
     for (i in 1:(length(x))){
       lines( c(x[i],x[i]), c(0, dnorm(x[i],m=m,sd=sd)), lty= 14, lwd=.2)
     }
   }


   dividers<- c(qnorm(0.025), -1, 0, 1, qnorm(0.975))
   addInteriorLine(mu+sigma*dividers, mu,sigma)

   # bquote creates an expression that text plotters can use
   t1<-  bquote( mu== .(mu))
   mtext(bquote( mu == .(mu)), 1, at=mu, line=-1)


   addInteriorLabel<- function(pos1, pos2, m, s){
     area<- abs(100*( pnorm(m+pos1*s,m,s)-pnorm(m+pos2*s, m,s)))
     mid<- m+0.5*(pos1+pos2)*s
     text(mid, 0.5*dnorm(mid,m,s),label=paste(round(area,2),"%"))
   }


   addInteriorLabel(dividers[1],dividers[2],  mu, sigma)
   addInteriorLabel(dividers[2],dividers[3],  mu, sigma)
   addInteriorLabel(dividers[3],dividers[4],  mu, sigma)
   addInteriorLabel(dividers[4],dividers[5],  mu, sigma)




### Following is problem point: axis will
### end up with correct labels, except for first point,
### where we end up with "b1" instead of "mu - 1.96*sigma".
    b1<- substitute( mu - d*sigma, list(d=*-round(dividers[1],2))*  )
    b2<- substitute( mu - sigma )
    b3<- substitute( mu )
    b4<- substitute( mu + sigma )
    b5<- substitute( mu + d*sigma, list(d=round(dividers[5],2)) )
##   plot(-20:50,-20:50,type="n",axes=F)
    axis(1, line=4,at=mu+dividers*sigma,
labels=*as.expression(c(b1,b2,b3,b4,b5))*, padj=-1)




### This gets "right result" but have to hard code the dividers
   b1<- expression( mu - 1.96*sigma )
   b2<- expression( mu - sigma )
   b3<- expression( mu )
   b4<- expression( mu + sigma )
   b5<- expression( mu + 1.96*sigma )
   axis(1, line=8,at=mu+dividers*sigma, labels=c(b1,b2,b3,b4,b5), padj=-1)




Allan

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to