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
|
#
# pairs.im.R
#
# $Revision: 1.22 $ $Date: 2021/06/29 03:43:35 $
#
pairs.listof <- pairs.solist <- function(..., plot=TRUE) {
argh <- expandSpecialLists(list(...), special=c("solist", "listof"))
names(argh) <- good.names(names(argh), "V", seq_along(argh))
haslines <- any(sapply(argh, inherits, what="linim"))
if(haslines) {
if(!requireNamespace("spatstat.linnet")) {
warning(paste("the pairs() plot for images on a linear network",
"requires the package 'spatstat.linnet'"),
call.=FALSE)
return(NULL)
}
do.call(spatstat.linnet::pairs.linim, append(argh, list(plot=plot)))
} else {
do.call(pairs.im, append(argh, list(plot=plot)))
}
}
pairs.im <- local({
allpixelvalues <- function(Z) { as.numeric(as.matrix(Z)) }
pairs.im <- function(..., plot=TRUE, drop=TRUE) {
argh <- list(...)
cl <- match.call()
## unpack single argument which is a list of images
if(length(argh) == 1) {
arg1 <- argh[[1]]
if(is.list(arg1) && all(unlist(lapply(arg1, is.im))))
argh <- arg1
}
## identify which arguments are images
isim <- unlist(lapply(argh, is.im))
nim <- sum(isim)
if(nim == 0)
stop("No images provided")
## separate image arguments from others
imlist <- argh[isim]
rest <- argh[!isim]
## determine image names for plotting
imnames <- argh$labels %orifnull% names(imlist)
if(length(imnames) != nim || !all(nzchar(imnames))) {
#' names not given explicitly
callednames <- paste(cl)[c(FALSE, isim, FALSE)]
backupnames <- paste0("V", seq_len(nim))
if(length(callednames) != nim) {
callednames <- backupnames
} else if(any(toolong <- (nchar(callednames) > 15))) {
callednames[toolong] <- backupnames[toolong]
}
imnames <- good.names(imnames, good.names(callednames, backupnames))
}
##
if(nim == 1) {
## one image: plot histogram
Z <- imlist[[1L]]
xname <- imnames[1L]
do.call(hist,
resolve.defaults(list(x=quote(Z), plot=plot),
rest,
list(xlab=xname,
main=paste("Histogram of", xname))))
## save pixel values
pixvals <- list(allpixelvalues(Z))
names(pixvals) <- xname
} else {
## extract pixel rasters and reconcile them
imwins <- solapply(imlist, as.owin)
names(imwins) <- NULL
rasta <- do.call(intersect.owin, imwins)
## convert images to common raster
imlist <- lapply(imlist, "[.im", i=rasta, raster=rasta, drop=FALSE)
## extract pixel values
pixvals <- lapply(imlist, allpixelvalues)
}
## combine into data frame
pixdf <- do.call(data.frame, pixvals)
## remove NA's
if(drop)
pixdf <- pixdf[complete.cases(pixdf), , drop=FALSE]
## pairs plot
if(plot && nim > 1)
do.call(pairs, resolve.defaults(list(x=quote(pixdf)),
rest,
list(labels=imnames, pch=".")))
labels <- resolve.defaults(rest, list(labels=imnames))$labels
colnames(pixdf) <- labels
class(pixdf) <- c("plotpairsim", class(pixdf))
return(invisible(pixdf))
}
pairs.im
})
plot.plotpairsim <- function(x, ...) {
xname <- short.deparse(substitute(x))
x <- as.data.frame(x)
if(ncol(x) == 1) {
x <- x[,1L]
do.call(hist.default,
resolve.defaults(list(x=quote(x)),
list(...),
list(main=xname, xlab=xname)))
} else {
do.call(pairs.default,
resolve.defaults(list(x=quote(x)),
list(...),
list(pch=".")))
}
return(invisible(NULL))
}
print.plotpairsim <- function(x, ...) {
cat("Object of class plotpairsim\n")
cat(paste("contains pixel data for", commasep(sQuote(colnames(x))), "\n"))
return(invisible(NULL))
}
panel.image <- function(x, y, ..., sigma=NULL) {
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
xx <- scaletointerval(x)
yy <- scaletointerval(y)
p <- ppp(xx, yy, window=square(1), check=FALSE)
plot(density(p, sigma=sigma), add=TRUE, ...)
}
panel.contour <- function(x, y, ..., sigma=NULL) {
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 1, 0, 1))
xx <- scaletointerval(x)
yy <- scaletointerval(y)
p <- ppp(xx, yy, window=square(1), check=FALSE)
Z <- density(p, sigma=sigma)
dont.complain.about(Z)
do.call(contour,
resolve.defaults(list(x=quote(Z), add=TRUE),
list(...),
list(drawlabels=FALSE)))
}
panel.histogram <- function(x, ...) {
usr <- par("usr"); on.exit(par(usr))
par(usr = c(usr[1:2], 0, 1.5) )
h <- hist(x, plot = FALSE)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- y/max(y)
do.call(rect,
resolve.defaults(list(xleft = breaks[-nB],
ybottom = 0,
xright = breaks[-1],
ytop = y),
list(...),
list(col="grey")))
}
## pairwise things like correlations
cov.im <- function(..., use = "everything",
method = c("pearson", "kendall", "spearman")) {
df <- pairs.im(..., plot=FALSE, drop=FALSE)
V <- cov(df, use=use, method=method)
return(V)
}
cor.im <- function(..., use = "everything",
method = c("pearson", "kendall", "spearman")) {
df <- pairs.im(..., plot=FALSE, drop=FALSE)
R <- cor(df, use=use, method=method)
return(R)
}
|