Skip to content

characters, mathematical expressions and computed values

5 messages · mee1d3hs, William Dunlap, David Winsemius

#
The code

hist(new, main=bquote("Heart Attack (" * bar(X)==.(round(mean(new),1)) *
")")) 

does not work on 2.15.2 on OSX. the histogram is generated, but the title
does show at all, which kind of tells me the bquote expression is failing

I would appreciate any help in making this work, it seems it should be easy,
but somehow it is not. I tried using substitute, and many other ways with no
help





--
View this message in context: http://r.789695.n4.nabble.com/characters-mathematical-expressions-and-computed-values-tp4645916p4650589.html
Sent from the R help mailing list archive at Nabble.com.
#
On Nov 23, 2012, at 12:20 PM, mee1d3hs wrote:

            
Works fine for me. I just updated to 2.15.2 today.

Used 1:4 as the value for new and got he correct title.
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: i386-apple-darwin9.8.0/i386 (32-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] survey_3.29     ggplot2_0.9.2.1

loaded via a namespace (and not attached):
  [1] colorspace_1.2-0   dichromat_1.2-4    digest_0.5.2
  [4] grid_2.15.2        gtable_0.1.1       labeling_0.1
  [7] MASS_7.3-22        memoise_0.1        munsell_0.4
[10] plyr_1.7.1         proto_0.3-9.2      RColorBrewer_1.0-5
[13] reshape2_1.2.1     scales_0.2.2       stringr_0.6.1
[16] tools_2.15.2


David Winsemius, MD
Alameda, CA, USA
#
I still can't get this to work, I am just trying to learn and this is
supposed to be a feature of R, the ability to combine math notation in
charts and exhibits

I did some more work to try to show what I am trying it to and what is not
working

 x <- rnorm(1000,mean=10,sd=2)
 
 par(mfcol=c(3,3))
 meanx <- round(mean(x),digits=2)
 
  x <- rnorm(1000,mean=10,sd=2)
 
 par(mfcol=c(4,2))
 meanx <- round(mean(x),digits=2)
 
 #chart 1:
 hist(x,main=paste(" 1. Mean of X =",bquote(.(meanx))))
 #chart 2:
 hist(x,main=paste(" 2. Mean of X = ",substitute(meanx,list(meanx=meanx))))
 #chart 3:
 hist(x,main=expression("3. "*bar(x)*" = "))
 #chart 4:
 hist(x,main=expression(paste("4. ",bar(x)," = ",bquote(.(meanx)))))
 #chart 5:
 hist(x,main=expression(paste("5. ",bar(x)," =
",substitute(meanx,list(meanx=meanx)))))
 #chart 6:
 hist(x,main=bquote(paste("6. ",expression(bar(x))," = ",.(meanx))))
 #chart 7:
 hist(x,main=paste("7. ",expression(bar(x))," =
",substitute(meanx,list(meanx=meanx))))
#chart 8:
 hist(x,main=bquote("Heart Attack (" * bar(X)==.(meanx) *")"))
      
 

what i want to do is to show a title that says X Bar = mean, where Bar X is
the mathematical notation for the mean X with a bar on top and mean is the
mean value

the first 2 charts on top work using bquote() or substitute(), but they
write the word 'mean' instead of X bar. The third one shows the X Bar with
no value (it was a first attempt and I never included the value)

charts 4 and 5 and 6 attempt to incorporate either bquote() or substitute
into #3 to make it work, in both cases it seems the bquote or the substitute
is not properly being evaluated. In 6 and 7 specifically I put the
expression inside the bquote or paste to see if reversing would work,
finally 8 is the one suggested with slight modifications to replace round
and mean for the variable with that result. (the output I get is attached)

I am hoping that someone can help me understand what I am doing wrong, and I
guess how the nested functions are being evaluated. I am learning to use R,
and while I may really never need to use math notation (in fact in my work I
may confuse people more with X bar than using mean), I believe there is
value to learning how to use the functions, I can see how they would be
useful for writing code.functions that write code so one can dynamically
solve problems. I have done that in SAS and I may do it in R

I appreciate anyones help. Sorry for the long post, but I wanted to show my
process to solve and learn what is going on

Mario


sample.png <http://r.789695.n4.nabble.com/file/n4650645/sample.png>  



--
View this message in context: http://r.789695.n4.nabble.com/characters-mathematical-expressions-and-computed-values-tp4645916p4650645.html
Sent from the R help mailing list archive at Nabble.com.
#
My message much earlier in the thread was
  > I think that bquote, with its .() operator, suffices for [almost?] any single title;
  > don't bother fiddling with expression(), substitute(), or parse().   (You can make
  > those work in many situations, but if you stick with just bquote then you can
  > spend your time on the title itself.)

I still think that is a good suggestion.  While you are learning this stuff, don't
even consider solutions that are not of the form
   main=bquote(...)
That leaves you with two of your nine attempts
  >  #chart 6:
  >  hist(x,main=bquote(paste("6. ",expression(bar(x))," = ",.(meanx))))
  > #chart 8:
  >  hist(x,main=bquote("Heart Attack (" * bar(X)==.(meanx) *")"))
I would ignore #6 as well because it has a call to expression() inside
the call to bquote.

What do you want to change about #8?

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
#
On Nov 24, 2012, at 5:20 AM, mee1d3hs wrote:

            
I sent a message yesterday that indicated that one of you earlier  
attempts was successful on my machine running the same (or roughly the  
same OS) and a current version of R. You seem to be ignoring the  
possibility that your R installation is broken.
This should "work", at least to the extent of putting nothing  after  
the equal sign. As should this:

hist(x,main=expression(3.~~bar(X)==phantom() ) )

The bquote function allows you to specify what portion of the  
expression will be evaluated. Otherwise everything gets displayed as  
typed (or interpreted by the plotmath evaluation engine). The tilde  
and asterisk are plotmath separators. Using them to bind together  
plotmath or text items will generally be much more readable than using  
paste.
It's either bquote() or expression(). Do not mix them unless you  
understand why they shouldn't be mixed.  Should be:

hist(x,main=bquote(4.~~bar(x)==.(meanx) ) )
This displays on my machine. (with "expression(.)" where the dot is a  
correctly drawn x-bar
)
That displays properly on my machine.