1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
## METHODS FOR GENERIC: image
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## NB: currently going via dgTMatrix in all cases,
## which is not so inefficient, as levelplot()
## needs 'i' and 'j' anyway
.image.dgT <-
function(x,
xlim = c(1, di[2L]),
ylim = c(di[1L], 1),
aspect = "iso",
sub = sprintf("Dimensions: %d x %d", di[1L], di[2L]),
xlab = "Column",
ylab = "Row",
cuts = 15,
useRaster = FALSE,
useAbs = NULL,
colorkey = !useAbs,
col.regions = NULL,
lwd = NULL,
border.col = NULL,
...)
{
## 'at' can remain missing and be passed to levelplot
di <- x@Dim
xx <- x@x
empty.x <- length(xx) == 0L && length(x) > 0L
if(empty.x) { # workaround having "empty" matrix
xx <- 0
x@i <- x@j <- 0L
}
if(missing(useAbs)) ## use abs() when all values are non-neg
useAbs <- if(empty.x) FALSE else min(xx, na.rm = TRUE) >= 0
else if(useAbs)
xx <- abs(xx)
## rx <- range(xx, finite = TRUE)
## FIXME: make use of 'cuts' now
## and call levelplot() with 'at = ',
## making sure 0 is included and matching
## *exactly* - rather than approximately
if(is.null(col.regions)) {
l.col <- empty.x || diff(rx <- range(xx, finite = TRUE)) == 0
col.regions <-
if(useAbs) {
grey(if(l.col)
0.9
else seq(from = 0.7, to = 0, length.out = 100L))
} else if(l.col)
"gray90"
else { ## no abs(.), rx[1] < 0 typically
nn <- 100
n0 <- min(nn, max(0, round((0 - rx[1L])/(rx[2L]-rx[1L]) * nn)))
col.regions <-
c(colorRampPalette(c("blue3", "gray80"))(n0),
colorRampPalette(c("gray75","red3"))(nn - n0))
}
}
if(!is.null(lwd) && !(is.numeric(lwd) && all(lwd >= 0))) # allow lwd=0
stop("'lwd' must be NULL or non-negative numeric")
stopifnot(length(xlim) == 2L, length(ylim) == 2L)
## ylim: the rows count from top to bottom:
ylim <- sort(ylim, decreasing = TRUE)
if(all(xlim == round(xlim))) xlim <- xlim + c(-0.5, +0.5)
if(all(ylim == round(ylim))) ylim <- ylim + c(+0.5, -0.5) # decreasing!
panel <-
if(useRaster)
panel.levelplot.raster
else {
function(x, y, z, subscripts, at, ..., col.regions) {
x <- as.numeric(x[subscripts])
y <- as.numeric(y[subscripts])
##
## FIXME: use level.colors() here and 'at' from above --
## ----- look at 'zcol' in panel.levelplot()
numcol <- length(at) - 1L
num.r <- length(col.regions)
col.regions <-
if(num.r <= numcol)
rep_len(col.regions, numcol)
else col.regions[1 + ((1:numcol-1)*(num.r-1)) %/% (numcol-1)]
zcol <- rep.int(NA_integer_, length(z))
for(i in seq_along(col.regions))
zcol[!is.na(x) & !is.na(y) & !is.na(z) &
at[i] <= z & z < at[i+1L]] <- i
zcol <- zcol[subscripts]
if(any(subscripts)) {
## the line-width used in grid.rect() inside
## levelplot()'s panel for the *border* of the
## rectangles: levelplot()panel has lwd= 0.01:
## Here: use "smart" default !
if(is.null(lwd)) {
wh <- current.viewport()[c("width", "height")]
## wh : current viewport dimension in pixel
wh <- (par("cra") / par("cin")) *
c(convertWidth(wh$width, "inches",
valueOnly = TRUE),
convertHeight(wh$height, "inches",
valueOnly = TRUE))
pSize <- wh/di ## size of one matrix-entry in pixels
pA <- prod(pSize) # the "area"
p1 <- min(pSize)
lwd <- ## crude for now
if(p1 < 2 || pA < 6) 0.01 # effectively 0
else if(p1 >= 4) 1
else if(p1 > 3) 0.5
else 0.2
## browser()
Matrix.message("rectangle size ",
paste(round(pSize, 1L), collapse = " x "),
" [pixels]; --> lwd :", formatC(lwd))
} else stopifnot(is.numeric(lwd), all(lwd >= 0)) # allow 0
if(is.null(border.col) && lwd < 0.01) # no border
border.col <- NA
grid.rect(x = x, y = y, width = 1, height = 1,
default.units = "native",
## FIXME?: allow 'gp' to be passed via '...' !!
gp = gpar(fill = col.regions[zcol],
lwd = lwd, col = border.col))
}
}
} # panel <- if(useRaster) ...
levelplot(xx ~ (x@j + 1L) * (x@i + 1L), # no 'data'
sub = sub,
xlab = xlab,
ylab = ylab,
xlim = xlim,
ylim = ylim,
aspect = aspect,
colorkey = colorkey,
col.regions = col.regions,
cuts = cuts,
par.settings = list(background = list(col = "transparent")),
panel = panel,
...)
}
setMethod("image", c(x = "dgTMatrix"), .image.dgT)
setMethod("image", c(x = "Matrix"),
function(x, ...) {
if(.M.kind(x) == "z")
stop(gettextf("%s(<%s>) is not yet implemented",
"image", "zMatrix"),
domain = NA)
image(.M2kind(.M2gen(.M2T(x)), "d"), ...)
})
setMethod("image", c(x = "CHMfactor"),
function(x, ...)
image(.M2gen(.M2T(expand1(x, "L"))), ...))
rm(.image.dgT)
|