2
votes

¿How do you print the ± sign in a bquote() expression in R?

I have tried the following:

pm
%pm%
±

These have not worked.


UPDATE #1 Here is some sample code

plot(NULL,xlim=c(0,10),ylim=c(0,10),xlab=NA,ylab=NA,xaxs="i",yaxs="i")
c <- "name"
p <- .004
n <- 969
b <- 1.23
s <- 0.45
tmp.txt <- paste(c(c," (n=",n,")\nslope = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
text(9.5,9.5,labels=tmp.txt,adj=c(1,1),cex=.75)

What I am trying to do is to make the 2nd line have beta (the symbol) instead of slope, and the ± symbol to appear. If I use expression, I can get the beta, but not the ±; if I just paste in ß (or something similar), it won't run.


UPDATE #2: It appears I HAVE to use bquote()...else the beta character won't print when piped out via pdf().

2
?plotmath suggests x %+-% y.r2evans
this did not work in the bquote()...¿does plotmath() go inside the bquote()? (the documentation on this function in R help is abysmal)Gregg H
update to last comment: there is no plotmath() function in R, so I'm even more at a loss how to proceed to solve thisGregg H
If you type ?plotmath in your R console, you will get a help page listing different math annotations you can use in bquote(). Use %+-% inside your bquote() call.Marius
according to the R help: "Control characters (e.g., \n) are not interpreted in character strings in plotmath, unlike normal plotting." So I'm wondering if this request is impossible in R.Gregg H

2 Answers

2
votes

An answer to this question suggests using paste with bquote. You could then use the Unicode character of ±:

x <- 232323
plot(1:10, main = bquote(paste(ARL[1], " curve for ", S^2, "; x=\U00B1",.(x))))

Note that this example (minus the inclusion of \U00B1) came from fabian's answer to the previously linked question.

0
votes

I appreciate the advice given, but it didn't fully accomplish my goal. Here is the workaround I came up with (and I personally think it is just short of asinine...but I'm at a loss).


    c <- "name"
    p <- .004
    n <- 969
    b <- 1.23
    s <- 0.45

    ## draw empty plot
    plot(NULL,xlim=c(0,10),ylim=c(0,10),xlab=NA,ylab=NA,xaxs="i",yaxs="i")

    ## place the "poor man's substitute"
    tmp.txt <- paste(c(c," (n=",n,")\nslope = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
    text(9.5,9.5,labels=tmp.txt,adj=c(1,1),cex=.75)

    ## place the next best option
    tmp.txt <- paste(c(c," (n=",n,")\n\U03B2  = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
    text(9.5,7.5,labels=tmp.txt,adj=c(1,1),cex=.75)

    ## place the two boxes to superimpose the bquote() version
    tmp.txt2 <- paste(c(c," (n=",n,")\n\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
    text(9.5,5.5,labels=tmp.txt2,adj=c(1,0.5),cex=.75)
    text(9.5,5.5,labels=bquote(beta == .(b)%+-%.(s)),adj=c(1,0.5,cex=.75))


    ## same as above, but piped to a *.pdf
    pdf("tmp_output.pdf")
     plot(NULL,xlim=c(0,10),ylim=c(0,10),xlab=NA,ylab=NA,xaxs="i",yaxs="i")
     tmp.txt <- paste(c(c," (n=",n,")\nslope = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
     text(9.5,9.5,labels=tmp.txt,adj=c(1,1),cex=.75)

     tmp.txt <- paste(c(c," (n=",n,")\n\U03B2  = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
     text(9.5,7.5,labels=tmp.txt,adj=c(1,1),cex=.75)

     tmp.txt2 <- paste(c(c," (n=",n,")\n\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")
     text(9.5,5.5,labels=tmp.txt2,adj=c(1,0.5),cex=.75)
     text(9.5,5.5,labels=bquote(beta == .(b)%+-%.(s)),adj=c(1,0.5,cex=.75))
    dev.off()

If you run this, it appears to work both inside of R and in the resulting *.pdf file.

As always, a more elegant (and sensible) solution would be much appreciated.