[R] ylim with only one value specified

Matthias Gondan matthias-gondan at gmx.de
Tue Oct 9 11:28:26 CEST 2012


Dear R developers,

I would like to have R choose the limits of the y-axis semi-automatically,
e.g., zero should be included, but the maximum should be chosen depending 
on the data.

Examples:

plot(1:10, 1:10) # selects min and max automatically
plot(1:10, 1:10, ylim=c(1, 10)) # manual definition
plot(1:10, 1:10, ylim=c(0, Inf)) # this would be a nice feature, i.e. lower y limit = 0 defined manually, upper limit = 10 selected automatically

The relevant code from plot.default would have to be modified:

old:

ylim <- if (is.null(ylim)) 
        range(xy$y[is.finite(xy$y)])
    else ylim

new:

ylim <- if (is.null(ylim)) 
        range(xy$y[is.finite(xy$y)])
    else if(length(ylim) == 2 & ylim[2] == Inf)
            c(ylim[1], max(xy$y[is.finite(xy$y)])
        else ylim

and some more if-else statements if cases like ylim=c(0, -Inf), 
ylim=c(Inf, 0), ylim=c(-Inf, 0) and ylim=c(-Inf, Inf)[as a replacement for NULL/
autoselection] and ylim=c(Inf, -Inf)[autoselection, reversed y axis] should be 
handled correctly.

I would find such a feature useful. Do you think it would interfere with other functions? 

Thank you for your consideration.

Best wishes,

Matthias Gondan



More information about the R-help mailing list