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
|
imagevect <- function (x, labels, contour = FALSE, gridsize = 20, axes = TRUE, nlabx = 5, nlaby = 5, ...) { require(fields) dimension <- function(x, unique = FALSE, sort = FALSE){ ncharX <- substring(x, 2, regexpr("Y", x)-1) ncharY <- substring(x, nchar(ncharX)+3, nchar(x)) if(unique){ ncharX = unique(ncharX) ncharY = unique(ncharY) } if(sort){ ncharX = sort(as.numeric(ncharX)) ncharY = sort(as.numeric(ncharY)) } res <- list(ncharX, ncharY) return(res) } formatXY <- function(x){ ncharX <- substring(x, 2, regexpr("Y", x)-1) ncharY <- substring(x, nchar(ncharX)+3, nchar(x)) resX <- c() for(i in 1:length(ncharX)){ n0x <- paste( rep(rep(0, length(ncharX[i])), times = (max(nchar(ncharX)) - nchar(ncharX[i]) + 1)), collapse = "", sep = "") resX[i] <- paste("X", substring(n0x, 2, nchar(n0x)), ncharX[i], collapse = "", sep = "") } resY <- c() for(i in 1:length(ncharY)){ n0x <- paste( rep(rep(0, length(ncharY[i])), times = (max(nchar(ncharY)) - nchar(ncharY[i]) + 1)), collapse = "", sep = "") resY[i] <- paste("Y", substring(n0x, 2, nchar(n0x)), ncharY[i], collapse = "", sep = "") } res <- paste(resX, resY, sep = "") return(res) } sort.x <- x[order(formatXY(labels))] rrr <- dimension(labels, unique = TRUE, sort = TRUE) dims <- c(length(rrr[[2]]), length(rrr[[1]])) dim(sort.x) <- dims par(xaxs = "i", yaxs = "i") image.plot(nnn <- t(sort.x), axes = FALSE, ...) if (contour) { contour(nnn, add = TRUE, ...) } if (axes) { points(0, 0, pch = " ", cex = 3) get.axis.ticks <- function(nlabs = NULL, gridsize = NULL, limit_max = NULL){ ngrid <- (limit_max-0)/gridsize per_grid <- 1/(ngrid-1) start <- 0 - (1/(ngrid-1))/2 stop <- 1 + (1/(ngrid-1))/2 lab <-(0:nlabs*(limit_max/nlabs)) at <- seq(from = start, to = stop, by = ((1 + per_grid))/((length(lab)-1))) return(list(lab, at)) } xaxis.position <- get.axis.ticks(nlabs = nlabx, gridsize = gridsize, limit_max = gridsize * nrow(nnn)) yaxis.position <- get.axis.ticks(nlabs = nlaby, gridsize = gridsize, limit_max = gridsize * ncol(nnn)) axis(1, labels = xaxis.position[[1]], at = xaxis.position[[2]]) axis(2, labels = yaxis.position[[1]], at = yaxis.position[[2]]) } invisible(nnn) }
|