[R] plus/minus +/- in factor; not plotmath not expression

David Winsemius dwinsemius at comcast.net
Mon Dec 2 22:12:20 CET 2013


On Dec 2, 2013, at 11:22 AM, Jacob Wegelin wrote:

> 
> I want to put the "plus or minus" symbol into a character variable, so that this can be turned into a factor and be displayed in the "strip" of a faceted ggplot2 plot.
> 
> A very nice solution, thanks to Professor Ripley's post of Nov 16, 2008; 3:13pm, visible at http://r.789695.n4.nabble.com/Symbols-to-use-in-text-td874239.html and subsequently http://www.fileformat.info/info/unicode/char/00b1/index.htm, is:
> 
> junk<- "\u00B1"
> print(junk)
> 
> #	This works very nicely. For instance:
> 
snipped code
> 
> This works very nicely on my system, but I just wanted to enquire:
> 
> Is this machine-independent and stable?

It is font-_dependent_. It displays fine on a Mac console if that's any help, but it seems you probably already know that. I tested it on a 2.15.3 version of R on a Windows XP machine which probably has the default font settings for that ancient OS and it displayed fine there, too. It really depends on whether the default font for you OS has a glyph in that position in its font table.

> 
> Is there a "native R" way to do this?
> 
> I did this in:
> 
>> sessionInfo()
> R version 2.15.3 (2013-03-01)
> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
>> 
> 
> Incidentally (and for the sake of keyword searches): Although a google search initially led me to posts about expression() and plotmath, those eventually had nothing to do with the solution.

That's not entirely true. The links on the ?plotmath page in the "Other symbols" section send you to ?points which has very instructive examples. I keep an annotated version of the output of TestChars(font=5) on the side of my desktop machine.

TestChars <- function(sign = 1, font = 1, ...)
{
   MB <- l10n_info()$MBCS
   r <- if(font == 5) { sign <- 1; c(32:126, 160:254)
       } else if(MB) 32:126 else 32:255
   if (sign == -1) r <- c(32:126, 160:255)
   par(pty = "s")
   plot(c(-1,16), c(-1,16), type = "n", xlab = "", ylab = "",
        xaxs = "i", yaxs = "i",
        main = sprintf("sign = %d, font = %d", sign, font))
   grid(17, 17, lty = 1) ; mtext(paste("MBCS:", MB))
   for(i in r) try( points(i%%16, i%/%16, pch = sign*i, font = font,...))
   for(i in r) try( text( (i%%16)-0.2, (i%/%16)-0.2,  as.character(i), font = 1, cex=0.5))
}

TestChars(font = 5)

You can see on that graphic that "±" is 177 and:
> strtoi(0x00B1)
[1] 177
> as.hexmode(177)
[1] "b1

-- 
David.
> 
> Jacob A. Wegelin


David Winsemius
Alameda, CA, USA



More information about the R-help mailing list