R/plotClusters.R

Defines functions plotClusters

Documented in plotClusters

#' Plot dendrograms for hierarchical clusering of systems or methods
#'
#' @param X a numeric matrix of error values
#' @param type (optional) a string describing if "systems" or "methods"
#'    are to be clustered. Default : "systems".
#' @param method (optional) a string describing the hierarchical
#'    clustering method. Default : "complete".
#' @param xlim (optional) a 2-vector of limits for the x-axis.
#' @param ylim (optional) a 2-vector of limits for the y-axis.
#' @param cex.lab (optional) a scale factor for the size of labels.
#' @param gPars a list of graphical parameters. See \link{setgPars}
#'
#' @return a plot
#' @export
#'
plotClusters <- function(
  X,
  type   = c("systems","methods"),
  method = c("complete","single"),
  xlim = NULL,
  ylim = NULL,
  cex.lab = 1,
  gPars = ErrViewLib::setgPars()
) {

  type   = match.arg(type)
  method = match.arg(method)

  # Expose gPars list
  for (n in names(gPars))
    assign(n, rlist::list.extract(gPars, n))

  if(type == "methods")
    X = t(X)

  cl = hclust(dist(X), method = method)
  hcd = as.dendrogram(cl)

  par(
    mfrow = c(1,1),
    mgp = mgp,
    pty = "m",
    mar = mar,
    tcl = tcl,
    cex = cex,
    lwd = lwd
  )

  plot(
    hcd,
    type  = "rectangle",
    hang  = -1,
    check = FALSE,
    nodePar = list(pch = NA, lab.cex = cex.lab, col=cols[1]),
    edgePar = list(col = cols[1], t.col=cols[1]),
    xlab  = toTitleCase(type),
    ylab  = "Height",
    xlim  = xlim,
    ylim  = ylim,
    main  = 'Hierarchical clustering dendrogram'
  )
  rect.hclust(
    cl,
    k = 2,
    border = cols[-1]
  )

}
ppernot/ErrViewLib documentation built on June 1, 2024, 4:33 a.m.