https://github.com/cran/fields
Raw File
Tip revision: 6c8b30169bba182a68765ee3cb9b4e2ef7d38332 authored by Doug Nychka on 16 November 2011, 00:00:00 UTC
version 6.6.3
Tip revision: 6c8b301
drape.color.R
# fields, Tools for spatial data
# Copyright 2004-2011, Institute for Mathematics Applied Geosciences
# University Corporation for Atmospheric Research
# Licensed under the GPL -- www.gpl.org/licenses/gpl.html
"drape.color" <- function(z, col = tim.colors(64), 
    zlim = NULL, breaks,  transparent.color = "white", midpoint = TRUE,
                          eps=1e-8) {
  # range if zlim not supplied
    if (is.null(zlim)) {
        zlim <- range(c(z), na.rm = TRUE)
    }
    # set any values outside of range to NA ( i.e. the transparent.color)
    z[(z < zlim[1]) | (z > zlim[2])] <- NA
    NC <- length(col)
    M <- nrow(z)
    N <- ncol(z)
    # if midpoint is TRUE find average z value for a facet and
    # overwrite z with matrix where row and column are one less
    # (reflecting that these are box centers not corners)
    if (midpoint) {
        z <- (z[1:(M - 1), 1:(N - 1)] + z[2:M, 1:(N - 1)] + z[1:(M - 
            1), 2:N] + z[2:M, 2:N])/4
        M<- M-1
        N<- N-1
    }
    if( missing( breaks)) { breaks<- NA}
    if( is.na(breaks[1])){
        # spacing for grid to assign  colors
        # +-eps included so that if z== zlim[1 or 2] it gets a color
        # if statement is for when the limit is exactly zero
        # thanks to Rosa Trancoso for finding this bug
        zrange<- zlim[2] - zlim[1]
        lower<- ifelse(abs(zlim[1])!=0, (zlim[1] - abs(zlim[1])*eps), -eps*zrange)
        upper<- ifelse(abs(zlim[2])!=0, (zlim[2] + abs(zlim[1])*eps), eps*zrange)
        breaks<- seq(lower, upper ,, NC+1)
    }
    if (length(breaks) != NC + 1){ 
          stop("must have one more break than colour")
    }
  # the magic of R ...
    icolor<-  cut(c(z),breaks)@.Data
  # returned values is a vector of character hex strings encoding the colors.
    hold<- ifelse(is.na(icolor), transparent.color, col[icolor])
  # points not assigned a bin from breaks get an NA
  # NA are converted to transparent color
    list( color.index=matrix(hold, nrow=M, ncol=N), breaks=breaks)
}
back to top