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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
## =============================================================================
## Contours in 3-D
## =============================================================================
createsegms <- function (x, y, z, colvar, names = c("x", "y", "z"),
dot, col, clim, dDepth, plist, levels, addbox) {
contour <- list(args = dot$points)
contour$args$col <- col
contour$args$levels <- levels
if (!ispresent(colvar))
stop ("'colvar' should be present for contour3D")
if (is.null(x))
x <- seq(0, 1, length.out = nrow(colvar))
if (is.null(y))
y <- seq(0, 1, length.out = ncol(colvar))
if (is.array(x) & length(dim(x)) == 1)
x <- as.vector(x)
if (is.array(y) & length(dim(y)) == 1)
y <- as.vector(y)
if (! is.vector(x))
stop(names[1], " should be a vector")
if (length(x) != nrow(colvar))
stop (names[1], " should be a vector of length = nrow(colvar) or be NULL")
if (! is.vector(y))
stop(names[2], " should be a vector")
if (length(y) != ncol(colvar))
stop (names[2], " should be a vector of length = ncol(colvar) or be NULL")
if (! is.matrix(z)) {
if (length(z) > 1)
stop("'z' should be a matrix or one value")
contour$side <- z
z <- colvar
} else {
if (length(x) != nrow(z))
stop(names[1], " should be of length = nrow(",names[3],")")
if (length(y) != ncol(z))
stop(names[2], " should be of length = ncol(",names[3],")")
contour$side <- "z"
}
# create contours
segm <- contourfunc(contour, x, y, z, plist, cv = colvar,
clim = clim, dDepth = dDepth, addbox = addbox)
names.from <- paste(names, ".from", sep = "")
names.to <- paste(names, ".to", sep = "")
names(segm)[1:6] <- c(names.from[1], names.to[1],
names.from[2], names.to[2],
names.from[3], names.to[3])
return(segm)
}
## =============================================================================
## main function
## =============================================================================
contour3D <- function(x = NULL, y = NULL, z = NULL, ...,
colvar = NULL, phi = 40, theta = 40,
col = NULL, colkey = NULL,
panel.first = NULL,
clim = NULL, clab = NULL, bty = "b",
dDepth = 1e-1, addbox = TRUE,
add = FALSE, plot = TRUE){
xlim <- ylim <- zlim <- c(0, 1)
if (!is.null(x))
xlim <- range(x, na.rm = TRUE)
if (!is.null(y))
ylim <- range(y, na.rm = TRUE)
if (!is.null(z))
zlim <- range(z, na.rm = TRUE)
plist <- initplist(add)
dot <- splitdotpersp(list(...), bty, FALSE, xlim, ylim, zlim,
plist = plist, breaks = NULL)
dots <- dot$points
levels <- dots$levels
dots$levels <- NULL
if (is.null(clim))
clim <- range(colvar, na.rm = TRUE)
clog <- FALSE
if (! is.null(dots$log)) {
if (length(grep("c", dots[["log"]])) > 0) {
dots[["log"]] <- gsub("c", "", dots[["log"]])
clog <- TRUE
}
if (dots[["log"]] == "")
dots[["log"]] <- NULL
}
if (clog) {
colvar <- log(colvar)
clim <- log(clim)
}
if (is.null(levels)) {
nlevs <- dots$nlevels
if (is.null(nlevs))
nlevs <- 10
if (clog)
levels <- exp(pretty(log(clim), nlevs))
else
levels <- pretty(clim, nlevs)
}
nlevs <- length(levels)
if (is.null(col))
col <- jet.col(nlevs)
if (! is.null(dot$alpha))
col <- setalpha(col, dot$alpha)
iscolkey <- is.colkey(colkey, col)
if (iscolkey)
colkey <- check.colkey(colkey)
if (is.null(plist)) {
do.call("perspbox", c(alist(xlim, ylim, zlim,
phi = phi, theta = theta, plot = plot,
colkey = colkey, col = col), dot$persp))
plist <- getplist()
}
if (is.function(panel.first))
panel.first(plist$mat)
isconstant <- NULL
ismatrix <- NULL
if (length(x) == 1)
isconstant <- c(isconstant, 1)
else if (is.matrix(x))
ismatrix <- c(ismatrix, 1)
if (length(y) == 1)
isconstant <- c(isconstant, 2)
else if (is.matrix(y))
ismatrix <- c(ismatrix, 2)
if (length(z) == 1)
isconstant <- c(isconstant, 3)
else if (is.matrix(z))
ismatrix <- c(ismatrix, 3)
if (length(isconstant) > 1)
stop ("only one of the values 'x' 'y', or 'z' can be one value")
if (length(ismatrix) > 1)
stop ("only one of the values 'x' 'y', or 'z' can be a matrix")
if (length(isconstant) > 1)
stop ("only one of the values 'x' 'y', or 'z' can be one value")
if (length(ismatrix) == 0 & length(isconstant) == 0)
stop ("exactly one of the values 'x' 'y', or 'z' should be a matrix or one value")
ismapped <- c(isconstant, ismatrix)
if (ismapped == 3) {
segm <- createsegms(x, y, z, colvar, c("x", "y", "z"), dot,
col, clim, dDepth, plist, levels, addbox)
} else if (ismapped == 1) {
segm <- createsegms(y, z, x, colvar, c("y", "z", "x"), dot,
col, clim, dDepth, plist, levels, addbox)
} else {
segm <- createsegms(x, z, y, colvar, c("x", "z", "y"), dot,
col, clim, dDepth, plist, levels, addbox)
}
if (iscolkey) {
if (is.null(colkey$at))
colkey$at <- levels
if (nlevs > 1) {
# for colors:
dz <- c(-diff(levels[1:2]), diff(levels[(nlevs-1):nlevs])) * 0.5
clim <- range(levels) + dz
}
plist <- plistcolkey(plist, colkey, col, clim, clab,
dot$clog, type = "contour3D", breaks = NULL)
}
plist <- plot_struct_3D(plist, segm = segm, plot = plot)
setplist(plist)
invisible(plist$mat)
}
|