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

Jacob Wegelin jacobwegelin at fastmail.fm
Mon Dec 2 20:22:24 CET 2013


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:

junk<-data.frame(gug=c(
 	rep( "\u00B1 1.2", 10)
 		,
 	rep( "\u00B1 2.3", 10)
 	)
)
junk$eks<-1:nrow(junk)
junk$why<-with(junk, as.numeric(gug) + eks)
print(summary(junk))
library(ggplot2)
print(
 	ggplot(data=junk, mapping=aes(x=eks, y=why))
 	+ geom_point()
 	+ facet_grid(. ~ gug)
)

This works very nicely on my system, but I just wanted to enquire:

Is this machine-independent and stable?

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)

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] ggplot2_0.9.3.1

loaded via a namespace (and not attached):
  [1] colorspace_1.2-0   dichromat_1.2-4    digest_0.6.0       grid_2.15.3        gtable_0.1.2       labeling_0.1
  [7] MASS_7.3-23        munsell_0.4        plyr_1.8           proto_0.3-10       psych_1.2.8        RColorBrewer_1.0-5
[13] reshape2_1.2.2     scales_0.2.3       stringr_0.6.2 
>

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.

Jacob A. Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
830 E. Main St., Seventh Floor
P. O. Box 980032
Richmond VA 23298-0032
U.S.A. 
CTSA grant: UL1TR000058
URL: http://www.people.vcu.edu/~jwegelin



More information about the R-help mailing list