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
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?
It is machine-independent and stable because \u00B1 means "Unicode
PLUS-MINUS SIGN", but it is not device-independent. There may be a
graphics device that does not support all Unicode characters. I'd
guess it is pretty widely available though.
Is there a "native R" way to do this?
That is native R.
Duncan Murdoch
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
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
There is also plotmath's '%+-%' operator:
plot(1,1, ylab = expression( A %+-% B ), xlab=expression( C%+-% D ) )
I noticed that Jacob was using ggplot2. Generally one can eventually find ways to label ggplot2 output with R expressions (used in the strict R language sense of the word), although sometimes it has been difficult for me to find the methods in the help pages.
David.
>
> Duncan Murdoch
>>
>> 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.
>>
>> Jacob A. Wegelin
>
David Winsemius
Alameda, CA, USA