John Maindonald
2007-Dec-30 23:45 UTC
[R] Symbolic substitution in parallel; use infinity symbol?
I'd like to be able to modify axlab in (C) below so that 'Inf' is replaced by the infinity symbol. y <- rnorm(40) breaks <- c(-Inf, -1, 1, Inf) x <- cut(y, breaks=breaks) plot(unclass(x), y, xaxt="n", xlab="") ## A: The following gives the axis labels "(-Inf, 1]", etc. axis(1, at=1:3, labels=expression("(-Inf,-1]", "(-1,1]", "(1, Inf]")) ## B: The following replaces Inf by the infinity symbol axis(1, at=1:3, labels=expression("(" * -infinity * ", " * -1 * "]", "(" * -1 * ", 1]", "(1, " * infinity * "]"), line=1.25, lty=0) ## C: The following gives the axis labels "(-Inf, 1]", etc., ## in a more automated manner. axlab <- lapply(levels(x), function(x)substitute(a, list(a=x))) # Can alternatively use bquote() axis(1, at=1:3, labels=as.expression(axlab), line=2.25, lty=0) However I have been unable to modify axlab so that the infinity symbol appears in place of 'Inf'. Is there is some relatively straightforward way to do this? The issue is of course more general than this specific example. John Maindonald email: john.maindonald at anu.edu.au phone : +61 2 (6125)3473 fax : +61 2(6125)5549 Centre for Mathematics & Its Applications, Room 1194, John Dedman Mathematical Sciences Building (Building 27) Australian National University, Canberra ACT 0200.
Peter Dalgaard
2007-Dec-31 01:35 UTC
[R] Symbolic substitution in parallel; use infinity symbol?
John Maindonald wrote:> I'd like to be able to modify axlab in (C) below so that 'Inf' > is replaced by the infinity symbol. > > y <- rnorm(40) > breaks <- c(-Inf, -1, 1, Inf) > x <- cut(y, breaks=breaks) > plot(unclass(x), y, xaxt="n", xlab="") > > ## A: The following gives the axis labels "(-Inf, 1]", etc. > axis(1, at=1:3, labels=expression("(-Inf,-1]", "(-1,1]", "(1, Inf]")) > > ## B: The following replaces Inf by the infinity symbol > axis(1, at=1:3, labels=expression("(" * -infinity * ", " * -1 * "]", > "(" * -1 * ", 1]", "(1, " * infinity > * "]"), > line=1.25, lty=0) > > ## C: The following gives the axis labels "(-Inf, 1]", etc., > ## in a more automated manner. > axlab <- lapply(levels(x), function(x)substitute(a, list(a=x))) > # Can alternatively use bquote() > axis(1, at=1:3, labels=as.expression(axlab), line=2.25, lty=0) > > However I have been unable to modify axlab so that the infinity > symbol appears in place of 'Inf'. Is there is some relatively > straightforward way to do this? The issue is of course more > general than this specific example. >Here's an idea, leaving some tinkering for you: breaks <- c(-Inf, -1, 1, Inf) zz <- lapply(breaks, function(x) if(x==-Inf) quote(-infinity) else if (x==Inf) quote(infinity) else format(x)) lbl <- mapply(function(x,y) bquote("(" * .(x) * "," * .(y) * "]"), zz[-4], zz[-1], SIMPLIFY=FALSE) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Bert Gunter
2007-Dec-31 15:42 UTC
[R] Symbolic substitution in parallel; use infinity symbol?
Happy New year John! Is the following what you are looking for: y <- rnorm(40) breaks <- c(-Inf, -1, 1, Inf) x <- cut(y, breaks=breaks) plot(unclass(x), y, xaxt="n", xlab="") leftlab <- expression("(-" * infinity * "]") rightlab<- expression("[+" * infinity * ")") axlab <- levels(x) axlab[1] <- leftlab axlab[length(axlab)] <- rightlab axis(1, at=1:3, labels=axlab, line=2.25, lty=0) Cheers, Bert Gunter Genentech -----Original Message----- From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org] On Behalf Of John Maindonald Sent: Sunday, December 30, 2007 3:45 PM To: r-help@stat.math.ethz.ch Subject: [R] Symbolic substitution in parallel; use infinity symbol? I'd like to be able to modify axlab in (C) below so that 'Inf' is replaced by the infinity symbol. y <- rnorm(40) breaks <- c(-Inf, -1, 1, Inf) x <- cut(y, breaks=breaks) plot(unclass(x), y, xaxt="n", xlab="") ## A: The following gives the axis labels "(-Inf, 1]", etc. axis(1, at=1:3, labels=expression("(-Inf,-1]", "(-1,1]", "(1, Inf]")) ## B: The following replaces Inf by the infinity symbol axis(1, at=1:3, labels=expression("(" * -infinity * ", " * -1 * "]", "(" * -1 * ", 1]", "(1, " * infinity * "]"), line=1.25, lty=0) ## C: The following gives the axis labels "(-Inf, 1]", etc., ## in a more automated manner. axlab <- lapply(levels(x), function(x)substitute(a, list(a=x))) # Can alternatively use bquote() axis(1, at=1:3, labels=as.expression(axlab), line=2.25, lty=0) However I have been unable to modify axlab so that the infinity symbol appears in place of 'Inf'. Is there is some relatively straightforward way to do this? The issue is of course more general than this specific example. John Maindonald email: john.maindonald@anu.edu.au phone : +61 2 (6125)3473 fax : +61 2(6125)5549 Centre for Mathematics & Its Applications, Room 1194, John Dedman Mathematical Sciences Building (Building 27) Australian National University, Canberra ACT 0200. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]