Bug 14987 - wishlist: image, persp, ..., findInterval to be used with decreasing x or y?
Summary: wishlist: image, persp, ..., findInterval to be used with decreasing x or y?
Status: NEW
Alias: None
Product: R
Classification: Unclassified
Component: Graphics (show other bugs)
Version: R 2.15.x
Hardware: All Windows 64-bit
: P5 enhancement
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2012-07-12 14:14 UTC by karline soetaert
Modified: 2012-07-12 14:14 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description karline soetaert 2012-07-12 14:14:05 UTC
# image, persp and findInterval only work with increasing x or y. 
# In certain cases, the data are given with descending values.
# Here is an example, and code that overcomes the limitation (but it may be too naive)  

y  <- seq(-4*pi, pi, len=10)
x  <- seq(4*pi, -2*pi, len=12)    # descending x
z <- sqrt(outer(x^2, y^2, "+"))


image(z, x = x, y = y)   # does not work although a monotonous series of x

# A suggestion to overcome it...
Image <- function (z, ...) {

   dots <- list(...)
   x <- dots[["x"]]
      if (all(diff(x) < 0)) {    # swap
        if (is.null(dots$xlim)) 
          dots$xlim <- rev(range(x))
        x <- rev(dots[["x"]])
        z <- z[nrow(z):1, ]
      }
   dots[["x"]]    <- NULL

   y <- dots[["y"]]
      if (all(diff(y) < 0)) {    # swap
        if (is.null(dots$ylim)) 
          dots$ylim <- rev(range(y))
        dots[["y"]] <- rev(dots[["y"]])
        z <- z[ ,ncol(z):1]
      }
   dots[["y"]]    <- NULL
   do.call("image", c(alist(x = x, y = y, z = z), dots))
}
Image(z, x = x, y = y)

# Similar for findInterval

findInterval(c(1, 3), x)   # does not work although a monotonous series of x

FindInterval <- function(x, vec, ...) {
      if (all(diff(vec) < 0)) {# swap
        vec <- rev(vec)
        c(length(vec):1) [findInterval(x, vec, ...)] -1
      } else findInterval(x, vec, ...) 
}
FindInterval(c(1, 3), x)