Skip to content
Snippets Groups Projects
exploratools.R 22.4 KiB
Newer Older
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
#############################################################################
#####     FUNCTIONS FOR SPATIAL COMPOSITIONAL EXPLORATORY ANALYSIS     ######
#############################################################################



#' Check presence of missings
#' check presence of missings in a data.frame
#' @param x data, of class data.frame
#' @param mc not used
#' @param ... not used
#'
#' @return A single true/false response about the presence of any missing value 
#' on the whole data set
#' @export
#' @importFrom compositions has.missings
#' @method has.missings data.frame
#'
#' @examples
#' library(compositions)
#' data(Windarling)
#' has.missings(Windarling)
#' head(Windarling)
#' Windarling[1,1] = NA
#' head(Windarling)
#' has.missings(Windarling)
has.missings.data.frame = function(x, mc=NULL, ...){
  if(is.null(x))
    return(FALSE)
  (!is.null(x)) && any(is.na(x))
}




#' Compositional maps, pairwise logratios
#' Matrix of maps showing different combinations of components of a composition, in pairwise logratios
#' @param loc matrix or data.frame of coordinates of the sample locations
#' @param comp composition observed at every location, can be a matrix, a data.frame or
#'  of one of the classes \code{compositions::acomp} or \code{compositions::aplus}
#' @param colscale set of colors to be used as colorscale (defauls to 10 colors between blue and red)
#' @param cexrange symbol size min and max values (default to 0.1 to 2)
#' @param scale function scaling the set of z-values of each map, defaults to \code{\link{rank}}
#' @param commonscale logical, should all plots share a common z-scale? defaults to FALSE
#' @param foregroundcolor color to be used for the border of the symbol
#' @param closeplot logical, should the plot be left open (FALSE) for further changes, or be frozen (TRUE)? 
#' defaults to TRUE
#'
#' @return The function is primarily called for producing a matrix of (D,D) plots of the D-part 
#' compositional samples, where at each plot we represent a map whose symbols are colored and 
#' sized according to a z-scale controlled by a different logratio. For each plot, this is the 
#' logratio of the row variable by the column variable. However, in case that `closeplot=FALSE`, 
#' this function returns 
#' invisibly the graphical parameters that were active prior to calling this function. This allows 
#' the user to add further stuff to the plots (mostly, using \code{par(mfg=c(i,j))} to plot on the
#' diagram (i,j)), or manually freeze the plot (by wrapping the call to \code{pwlrmap} on a call to 
#' \code{\link{par}}).
#' @export
#' @importFrom graphics plot text mtext
#'
#' @examples
#' 
#' data("Windarling")
#' coords = as.matrix(Windarling[,c("Easting","Northing")])
#' compo = Windarling[,c("Fe","Al2O3","SiO2", "Mn", "P")]
#' compo$Rest = 100-rowSums(compo)
#' compo = compositions::acomp(compo) 
#' # in quantiles (default, ranking controls color and size)
#' pwlrmap(coords, compo) 
#' \donttest{
#' # in logratios (I=identity)
#' pwlrmap(coords, compo, scale=I)
#' # in ratios (i.e., apply exp)
#' pwlrmap(coords, compo, scale=exp)  
#' # use only color, no change in symbol size
#' pwlrmap(coords, compo, cexrange=c(1,1)) 
#' # change all
#' pwlrmap(coords, compo, commonscale=TRUE, cexrange=c(1.2,1.2), 
#'                     colscale=rev(rainbow(40, start=0, end=4/6))) 
#' }
pwlrmap = function(loc,   # XY coordinates (matrix or data frame)
                   comp,  # composition (matrix, acomp, aplus or data.frame)
                   colscale=rev(rainbow(10, start=0, end=4/6)), # color scale (defauls to 10 colors between blue and red)
                   cexrange=c(0.1, 2),  # symbol size min and max values (default to 0.1 to 2)
                   scale=rank, # scaling FUNCTION (defaults to ranking, i.e. quantiles)
                   commonscale=FALSE,  # should all plots be generated with a common scale?
                   foregroundcolor="black",
                   closeplot = TRUE
                   ){
  # set of maps where the symbols are chosen according to each possible pwlr, in 
  # a scale given by the user
  opar = par()
  opar = par_remove_readonly(opar)
  
  if(closeplot) on.exit(par(opar))
  # dimensions
  D = ncol(comp)
  N = nrow(loc)
  # compute pwlrs
  ij = expand.grid(i=1:D, j=1:D) # pairs of indices
  mat = matrix(0, nrow=D, ncol=D*D)   # matrix of +1 numerator, -1 denominator
  for(k in 1:nrow(ij)){
    mat[ij[k,1],k]=-1
    mat[ij[k,2],k]=1
  }
  Zpwlr = as.matrix(log(unclass(comp))) %*% mat
  # scale the variables
  if(commonscale){
    sizes  = scale(c(unlist(Zpwlr)))
    dim(sizes) = dim(Zpwlr)
    # calculate common cutting levels
    commonbks = seq(from=min(sizes[is.finite(sizes)]), to=max(sizes[is.finite(sizes)]), length.out=length(colscale))
    dfbks = diff(commonbks)[1]
    commonbks = c(commonbks[1]-dfbks, commonbks+0.5*dfbks)
  }else{
    sizes = gmApply(Zpwlr,2,scale)    
  }
  # do the plot
  par(mfrow=c(D,D), mar=c(1,1,1,1)/5, oma=c(3,5,5,3))
  for(i in 1:D){
    for(j in 1:D){
      if(i==j){
        # diagonal plots, only labels
        plot(loc, type="n", xaxt=ifelse(i==D,"s","n"), yaxt=ifelse(j==1,"s","n") )
        text(mean(range(loc[,1])), mean(range(loc[,2])), colnames(comp)[i], cex=2)
      }else{
      # off-diagonal plots, maps  
      k = (i-1)*D+j        
      sz = sizes[,k]  
      # choose which breaks to use, the common ones or the particular ones
      if(commonscale){
        bks = commonbks
      }else{
       bks = seq(from=min(sz[is.finite(sz)]), to=max(sz[is.finite(sz)]), length.out=length(colscale))
         dfbks = diff(bks)[1]
       bks = c(bks[1]-dfbks, bks+0.5*dfbks)
      }
      # compute colors and sizes
      cols = colscale[cut(sz, bks)]
      sz = cexrange[1]+(cexrange[2]-cexrange[1])*(sz-bks[1])/c(range(bks) %*% c(-1,1))
      # actual map
      plot(loc, cex=sz, bg=cols, pch=21, asp=1, col=foregroundcolor,
            xaxt=ifelse(i==D,"s","n"),yaxt=ifelse(j==1,"s","n")
           )
      }
      # add axes on extra sides
      if(i==1) axis(side=3)
      if(j==D) axis(side=4)
    }
  }
  # add labels
  mtext("numerator", side=2, outer=TRUE, line=2.5, cex=1.25)
  mtext("denominator", side=3, outer=TRUE, line=2.5, cex=1.25)
  # return the old graphical parameters to freeze the plot
  invisible(opar)
}






#' Multiple maps
#' Matrix of maps showing different combinations of components of a composition, user defined
#' 
#'
#' @param data data to represent; admits many data containing objects 
#' (matrix, data.frame, classes from package \code{compositions}) as well 
#' as their \code{Spatial} counterparts (in which case, \code{loc} is not necessary)
#' @param ... extra arguments for generic functionality
#'
#' @return The function is primarily called for producing a matrix of plots of each component of a 
#' multivariate data set, such as a compositional data set. Each plot represents a map whose symbols 
#' are colored and sized according to a z-scale controlled by one of the variables of the data set. 
#' It can be used virtually with any geometry, any kind of data (compositional, positive, raw, etc). 
#' This function returns invisibly the graphical parameters that were active prior to calling this 
#' function. This allows the user to add further stuff to the plots (mostly, using \code{par(mfg=c(i,j))} 
#' to plot on the diagram (i,j)), or else freeze the plot (by wrapping the call to \code{pwlrmap} 
#' on a call to \code{\link{par}}).
#' @export
#' @importFrom grDevices rainbow
#' @importFrom graphics par plot
#'
#' @examples
#' data("Windarling")
#' library("compositions")
#' coords = as.matrix(Windarling[,c("Easting","Northing")])
#' compo = Windarling[,c("Fe","Al2O3","SiO2", "Mn", "P")]
#' compo$Rest = 100-rowSums(compo)
#' compo = acomp(compo)
#' pairsmap(data=clr(compo), loc=coords) # clr
#' pairsmap(data=compo, loc=coords) # closed data
pairsmap <- function(data, ...) UseMethod("pairsmap", data)


#' @describeIn pairsmap Multiple maps
#' @method pairsmap SpatialPointsDataFrame
pairsmap.SpatialPointsDataFrame <- function(data, ...){
  pairsmap.default(data@data, loc=sp::coordinates(data), ...)
}


#' @describeIn pairsmap Multiple maps
#' @method pairsmap default
#' @export
#' @param loc matrix or data.frame of coordinates of the sample locations
#' @param colscale set of colors to be used as colorscale (defauls to 10 colors between blue and red)
#' @param cexrange symbol size min and max values (default to 0.1 to 2)
#' @param scale function scaling the set of z-values of each map, defaults to \code{\link{rank}}
#' @param commonscale logical, should all plots share a common z-scale? defaults to FALSE
#' @param mfrow vector of two integers, giving the number of plots per row and per column of the 
#' matrix of plots to be created; defaults to something square-ish, somewhat wider than longer, and able to
#' contain all plots
#' @param foregroundcolor color to be used for the border of the symbol
#' @param closeplot logical, should the plot be left open (FALSE) for further changes, or be frozen (TRUE)? 
#' defaults to TRUE
pairsmap.default <- function(data,   # data to represent
                    loc,   # XY coordinates (matrix or data frame)
                   colscale=rev(rainbow(10, start=0, end=4/6)), # color scale (defauls to 10 colors between blue and red)
                   cexrange=c(0.1, 2),  # symbol size min and max values (default to 0.1 to 2)
                   scale=rank, # scaling FUNCTION (defaults to ranking, i.e. quantiles)
                   commonscale=FALSE,  # should all plots be generated with a common scale?
                   mfrow = c(floor(sqrt(ncol(data))), ceiling(ncol(data)/floor(sqrt(ncol(data))))),# automatic distribution of figs
                   foregroundcolor = "black", 
                   closeplot=TRUE,
                   ...
){
  opar = par()
  opar = par_remove_readonly(opar)
  if(closeplot) on.exit(par(opar))
  # dimensions
  D = ncol(data)
  N = nrow(loc)
  # scale the variables
  if(commonscale){
    sizes  = scale(c(unlist(data)))
    dim(sizes) = dim(data)
    # calculate common cutting levels
    commonbks = seq(from=min(sizes[is.finite(sizes)]), to=max(sizes[is.finite(sizes)]), length.out=length(colscale))
    dfbks = diff(commonbks)[1]
    commonbks = c(commonbks[1]-dfbks, commonbks+0.5*dfbks)
  }else{
    sizes = gmApply(data,2,scale)    
  }
  # do the plot
  par(mfrow=mfrow, mar=c(1,1,10,1)/5, oma=c(3,3,3,3))
  drow = mfrow[1]
  dcol = mfrow[2]
  for(i in 1:drow){
    for(j in 1:dcol){
        # off-diagonal plots, maps  
        k = (i-1)*dcol+j        
        if(k<=D){
         sz = sizes[,k]  
         # choose which breaks to use, the common ones or the particular ones
         if(commonscale){
           bks = commonbks
         }else{
            bks = seq(from=min(sz[is.finite(sz)]), to=max(sz[is.finite(sz)]), length.out=length(colscale))
           dfbks = diff(bks)[1]
           bks = c(bks[1]-dfbks, bks+0.5*dfbks)
         }
         # compute colors and sizes
         cols = colscale[cut(sz, bks)]
         sz = cexrange[1]+(cexrange[2]-cexrange[1])*(sz-bks[1])/c(range(bks) %*% c(-1,1))
         # actual map
         plot(loc, cex=sz, bg=cols, pch=21, asp=1, main=colnames(data)[k], col=foregroundcolor,
             xaxt=ifelse(i==drow,"s","n"),yaxt=ifelse(j==1,"s","n")
         )
        } 
       # add axes on extra sides
       # if(i==1) axis(side=3)
       if(j==dcol) axis(side=4)
    }
  }
  # return the old graphical parameters
  #par(mfrow=opar$mfrow, mar=opar$mar, oma=opar$oma)
  invisible(opar)
}



#' Spectral colors palette
#' based on the RColorBrewer::brewer.pal(11,"Spectral")
#'
#' @param n number of colors 
#'
#' @return a palette, i.e. a list of colors, from dark blue to dark red over pale yellow.
#' @export
#' @importFrom grDevices colorRampPalette
#' @import RColorBrewer
#'
#' @examples
#' (cls=spectralcolors(20))
spectralcolors <- function(n){
  cls = RColorBrewer::brewer.pal(min(11,n), "Spectral")
  if(n>11){
    cls = grDevices::colorRampPalette(cls)(n)
  }
  return(rev(cls))
}

# #### MOVED TO COMPOSITIONS #####
# Compositional panel 1D-density plot
# Panel minifunction for plotting histograms and kernel densities of the data 
#
# @param x ignored (here for compatibility with \code{\link{qqnorm.acomp}})
# @param y numeric vector of response values
# @param col color of the plot
# @param ... further parameters to plotting functions, currently ignored
# @param alpha alpha level at which the test should be done
#
# @return If given to the argument \code{panel} of a function such as \code{\link{qqnorm.acomp}}),
# this produces a matrix of plots where each panel contains a histogram and a kernel density 
# overdrawn. If the distribution of this data is accepted to be normal at the \code{alpha}-level
# by a \code{\link{shapiro.test}}, then the histogram is painted with the \code{col}or provided; 
# otherwise the histogram bars are empty, but the kernel density curve is colored.
#
# @examples
# data("Windarling")
# compo = as.matrix(Windarling[,c("Fe","Al2O3","SiO2", "Mn", "P")])
# qqnorm.acomp(compo, panel=vp.lrdensityplot, alpha=0.05) 
# #### MOVED TO COMPOSITIONS #####





# Panel function for 2D-density plots
# Panel minifunction for plotting 2D kernel densities of the data 
#
# @param x numeric vector of response values (axis X)
# @param y numeric vector of response values (axis Y)
# @param xaxt style of the X axis labelling (defaults to "n", none)
# @param yaxt style of the Y axis labelling (defaults to "n", none)
# @param grid logical, should a unit grid be drawn? defaults to TRUE
# @param legpos string, position of the correlation coefficient, defaults to "bottomright"
# @param add logical, should the output be added to an existing diagram? defaults to TRUE, as required for acting as a panel diagram
# @param colpalette color palette for the image (defaults to spectral colors: blue-yellow-red)
# @param ... further parameters to image
#
# @return If given to the argument \code{panel} of a function such as \code{\link{pairs}}),
# this produces a matrix of images where each panel contains a 2D kernel density map,
# using blue for low density regions and dark red for high density colors. 
# Regression lines (y~x) and correlation coefficients are added as well.
# @export
#
# @examples
# data("Windarling")
# compo = Windarling[,c("Fe","Al2O3","SiO2", "Mn", "P")]
# pairs(clr(compo), panel=vp.kde2dplot)
# vp.kde2dplot = 
#   function(x, y, xaxt="n", yaxt="n",
#            grid=TRUE,legpos="bottomright", add=TRUE, colpalette=spectralcolors,...){
#     aux = MASS::kde2d(x, y, n=50)
#     aux$z = sqrt(aux$z)
#     bks = hist(aux$z, plot=F, breaks=20)$breaks
#     cols = c(NA,colpalette(length(bks)-2))
#     image(aux, breaks = bks, col=cols, xlab="", ylab="", xaxt=xaxt, yaxt=yaxt,add=add, ...) #yaxt=ifelse(j==1,"s","n")
#     xgrid = seq(from=floor(min(x)), to = ceiling(max(x)), by=1)
#     ygrid = seq(from=floor(min(y)), to = ceiling(max(y)), by=1)
#     abline(lm(y~x), col=2, lwd=2)
#     if(grid)abline(v=xgrid, h=ygrid, col="#999999")
#     legend(legpos, legend=round(cor(x,y), dig=3), bg="#999999")
#   }





#' Swath plots
#' 
#' Plots of data vs. one spatial coordinate, with local average spline curve
#'
#' @param data data to be represented, compositional class, data.frame, or
#' spatial data object (in which case, \code{loc} is a formula!)
#' @param ... further arguments to panel plots
#'
#' @return Nothing, as the function is primarily called to produce a plot
#' @export
#' @importFrom stats loess
#' @importFrom graphics plot par text lines mtext
#'
#' @examples
#' \donttest{
#' data("Windarling")
#' library("sp")
#' compo = compositions::acomp(Windarling[,c("Fe","Al2O3","SiO2", "Mn", "P")])
#' Northing = Windarling$Northing
#' swath(compo, Northing)
#' wind.spdf = sp::SpatialPointsDataFrame(sp::SpatialPoints(Windarling[,6:7]), 
#'      data=compo)
#' swath(wind.spdf, loc=Northing)
#' }
swath <- function(data, ...) UseMethod("swath", data)



#' @describeIn swath swath plot default method
#' @export
#' @method swath default
#' @param loc a numeric vector with the values for one coordinate
#' @param pch symbol to be used for the individual points, defaults to "."
#' @param withLoess either logical (should a loess line be added?) or a list 
#' of arguments to DescTools::lines.loess
#' @param commonScale logical or NA: should all plots share the same vertical 
#' range? FALSE=no, TRUE=yes (default), for
#' compositional data sets, the value NA (=all plots within a row) is also 
#' permitted and is actually the default
#' @param xlab label for the common x axis (defaults to a deparsed version 
#' of loc)
#' @param mfrow distribution of the several plots; it has a good internal default 
#' (not used for compositional 
#' classes)
swath.default <- function(data,  # data (matrix, rmult, aplus, rplus or data.frame)
                           loc,   # X or Y coordinates (a vector)
                           pch=".",
                           withLoess=TRUE, # either logical (should a loess line be added?) or a list of arguments to lines.loess
                           commonScale=TRUE, # logical: should all plots share the same vertical range? FALSE=no, TRUE=yes
                           xlab = deparse(substitute(loc)),
                           ..., # extra arguments to plot
                           mfrow # automatic distribution of figs
){
  # set of swath plots for each possible pwlr, eventually with a loess line 
  if(missing(mfrow)) mfrow = c(floor(sqrt(ncol(data))), ceiling(ncol(data)/floor(sqrt(ncol(data)))))
  if(is(data, "Spatial")) return(swath_SpatialPointsDataFrame(data=data, loc=loc, pch=pch, xlab=xlab, mfrow=mfrow,
                                                    withLoess=withLoess, commonScale=commonScale, ...))
  # preparations
  opar = par()
  opar = par_remove_readonly(opar)
  
  on.exit(par(opar))
  col0 = spectralcolors(10)[10]
  
  # isometric representation
  if(is(data, "data.frame")) data = compositions::rmult(as.matrix(data), V=gsi.getV(data), orig=gsi.orig(data))
  comp = idt(data)
  
  # dimensions
  D = ncol(comp)
  N = length(loc)
  
  # scale the variables
  if(is.na(commonScale)){
    rgY = rep(range(unclass(comp), na.rm=TRUE), D)
    dim(rgY) = c(2,D)
  }else{
    rgY = rep(NA, 2*D) 
    dim(rgY) = c(2,D)
  }
  
  # do the plot
  par(mfrow=mfrow, mar=c(1,1,3,1)/5, oma=c(5,5,5,3))
  for(k in 1:D){
    ij = par()$mfg
    i = ij[1]
    j = ij[2]
    plot(loc, comp[,k], ylim = range(c(comp[,k], rgY[,i]), na.rm=TRUE), pch = pch,
         xaxt = ifelse(i==D,"s","n"), yaxt = ifelse(j==1,"s","n"),...)
    title(main=colnames(comp)[k], line=0.5)
    if(is.logical(withLoess)){
      if(withLoess & requireNamespace("DescTools")){
        a = stats::loess(comp[,k]~loc)
        DescTools::lines.loess(a, col=col0)
      }
    }else if(is(withLoess, "list") & requireNamespace("DescTools")){
      args = withLoess
      if(!("col" %in% names(args))) args$col = col0
      args$x = stats::loess(comp[,k]~loc)
      do.call("DescTools::lines.loess", args=args)
    }
    # add axes on extra sides
    if(i==1) axis(side=3)
    if(j==D) axis(side=4)
  }
  # add labels
  mtext(xlab, side=1, outer=TRUE, line=2.5, cex=1.25)
  # return the old graphical parameters to freeze the plot
  #invisible(opar)
}


#' @describeIn swath  Swath plots for acomp objects
#' @method swath acomp 
#' @export
#' @importFrom compositions acomp
swath.acomp <- function(data,  # composition (rcomp, acomp, ccomp)
           loc,   # X or Y coordinates (a vector)
           pch = ".",
           withLoess = TRUE, # either logical (should a loess line be added?) or a list of arguments to lines.loess
           commonScale = NA, # logical or NA:
           xlab = deparse(substitute(loc)),
           ... # extra arguments to plot
  ){
    # recover byRowCommonScale from commonScale
    byRowCommonScale = ifelse(is.na(commonScale), TRUE, ifelse(commonScale, NA, FALSE))
    # set of swath plots for each possible pwlr, eventually with a loess line 

    # preparations
    opar = par()
    opar = par_remove_readonly(opar)
    
    on.exit(par(opar))
    col0 = spectralcolors(10)[10]
    comp = data
    
    # dimensions
    D = ncol(comp)
    N = length(loc)

    # compute pwlrs
    ij = expand.grid(i=1:D, j=1:D) # pairs of indices
    mat = matrix(0, nrow=D, ncol=D*D)   # matrix of +1 numerator, -1 denominator
    for(k in 1:nrow(ij)){
      mat[ij[k,1],k]=-1
      mat[ij[k,2],k]=1
      if(ij[k,1]==ij[k,2]) mat[ij[k,1],k]=0
    }
    Zpwlr = unclass(as.matrix(cdt(comp))) %*% mat
    
    # scale the variables
    if(is.na(byRowCommonScale)){
      rgY = rep(range(Zpwlr, na.rm=TRUE), 2*D)
      dim(rgY) = c(2,2*D)
    }else{
      if(byRowCommonScale){
        rgY = sapply(1:D, function(i)  range(Zpwlr[,(D*(i-1)+1:D)[-i]], na.rm=TRUE))
      }else{
        rgY = rep(NA, 2*D)
        dim(rgY) = c(2,D)
      }
    }
    
    # do the plot
    par(mfrow=c(D,D), mar=c(1,1,1,1)/5, oma=c(5,5,5,3))
    for(i in 1:D){
      for(j in 1:D){
        k = (i-1)*D+j        
        if(i==j){
          # diagonal plots, only labels
          plot(loc, Zpwlr[,k], type="n", xaxt=ifelse(i==D,"s","n"), yaxt=ifelse(j==1,"s","n") )
          text(mean(range(loc, na.rm=TRUE)), 0, colnames(comp)[i], cex=2)
        }else{
          # off-diagonal plots, swaths  
          plot(loc, Zpwlr[,k], ylim = range(c(Zpwlr[,k], rgY[,i]), na.rm=TRUE), pch=pch,
               xaxt=ifelse(i==D,"s","n"), yaxt=ifelse(j==1,"s","n"), ...)
          if(is.logical(withLoess)){
            if(withLoess){
              a = stats::loess(Zpwlr[,k]~loc)
              DescTools::lines.loess(a, col=col0)
            }
          }else if(is(withLoess, "list")){
            args = withLoess
            if(!("col" %in% names(args))) args$col = col0
            args$x = stats::loess(Zpwlr[,k]~loc)
            do.call("DescTools::lines.swath", args=args)
          }
        }
        # add axes on extra sides
        if(i==1) axis(side=3)
        if(j==D) axis(side=4)
      }
    }
    # add labels
    label1 = c("+ part", "numerator") [1+is.acomp(data)]
    label2 = c("- part", "denominator") [1+is.acomp(data)]
    mtext(label1, side=2, outer=TRUE, line=2.5, cex=1.25)
    mtext(label2, side=3, outer=TRUE, line=2.5, cex=1.25)
    mtext(xlab, side=1, outer=TRUE, line=2.5, cex=1.25)
    # return the old graphical parameters to freeze the plot
    #invisible(opar)
}

#' @describeIn swath Swath plots for ccomp objects
#' @method swath ccomp
#' @export
#' @importFrom compositions ccomp
swath.ccomp <- swath.acomp

#' @describeIn swath Swath plots for rcomp objects
#' @method swath rcomp 
#' @export
#' @importFrom compositions rcomp
swath.rcomp <- swath.acomp




swath_SpatialPointsDataFrame <- function(data, loc, ...){
  swath(data@data, loc=loc, ...)
}