Gregg H
Gregg H

Reputation: 178

How to print plus-minus and beta signs in bquote, and correctly export to pdf

¿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().

Upvotes: 2

Views: 2569

Answers (2)

Gregg H
Gregg H

Reputation: 178

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.

Upvotes: 0

user387832
user387832

Reputation: 503

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.

Upvotes: 3

Related Questions