2

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

smci
  • 32,567
  • 20
  • 113
  • 146
Gregg H
  • 170
  • 1
  • 9
  • 1
    [`?plotmath`](https://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/plotmath.html) suggests `x %+-% y`. – r2evans May 29 '18 at 03:40
  • 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 May 29 '18 at 03:42
  • update to last comment: there is no plotmath() function in R, so I'm even more at a loss how to proceed to solve this – Gregg H May 29 '18 at 03:44
  • 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 May 29 '18 at 03:48
  • 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 May 29 '18 at 04:05

2 Answers2

3

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.

user387832
  • 503
  • 3
  • 8
  • I am trying to get the bquote() text to appear on the middle line...and I cannot figure out a was to use the paste() function to accomplish this. – Gregg H May 29 '18 at 04:00
  • Do you have to use `bquote`? Given your update, does `tmp.txt <- paste(c(c," (n=",n,")\n\U03B2 = ",b,"±",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")` solve your problem? – user387832 May 29 '18 at 04:02
  • yes, this worked...now here's to hoping I never have to ever use bquote()...thnx – Gregg H May 29 '18 at 04:08
  • update...this did NOT work...it displayed correctly inside of R, but when piping the graphic to a file with pdf(), it won't plot the beta – Gregg H May 29 '18 at 04:11
  • How are you piping the plot? When I use `pdf("plot.pdf")`, it works on my machine. – user387832 May 29 '18 at 04:15
  • pdf(file="tmp_plot_02a.pdf",width=6.5,height=4.5) – Gregg H May 29 '18 at 04:20
  • My bad, change to `tmp.txt <- paste(c(c," (n=",n,")\n\U03B2 = ",b,"\U00B1",s,"\n",ifelse(p==0,"p<.001",paste0("p=",p))),collapse="")`. You need the unicode character of the plus-minus sign. – user387832 May 29 '18 at 04:27
  • thnx, but the problem was with the beta not printing in the *.pdf (the ± prints fine either way) – Gregg H May 29 '18 at 04:35
  • The only other reason I can think of that it would work on my machine and not yours is due to the default encoding. A work-around might be to use `cairo_pdf` (or `cairoPDF`) instead of `pdf` as explained in https://stackoverflow.com/questions/31075826/export-plot-as-pdf-with-a-utf8-character. If that doesn't work, I'm not sure what else I can suggest. – user387832 May 29 '18 at 04:51
0

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.

Gregg H
  • 170
  • 1
  • 9