[R] Symbolic substitution in parallel; use infinity symbol?

John Maindonald John.Maindonald at anu.edu.au
Wed Jan 2 01:01:49 CET 2008


Actually this works beautifully as it stands:

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[-length(zz)], zz[-1], SIMPLIFY=FALSE)

y <- rnorm(40)
x0 <- cut(y, breaks=breaks)
plot(x0, y, xaxt="n")

axis(1, at=1:3, labels=as.expression(lbl))

Thanks, Peter.

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.


On 31 Dec 2007, at 12:35 PM, Peter Dalgaard wrote:

> 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
>
>




More information about the R-help mailing list