
# Creation: 6/2016
# Author: Nicola Cortesi
# Aim: it's like ColorBar, but the user can specify a subset of labels (option 'subset') to be shown in the legend instead of the predefined values.
#      It's especially useful when you have to publish a chart for a paper/report and you want to see in the legend only the values you want, instead of those decided automatically by ColorBar()
# I/O: as for ColorBar()
# Assumptions: none
# Branch: general

ColorBarCustom <- function (brks, cols = NULL, vert = TRUE, subsampleg = 1, cex = 1, my.ticks, my.labels) 
{
    if (is.null(cols) == TRUE) {
        nlev <- length(brks) - 1
        cols <- rainbow(nlev)
    }
    else {
        if (length(cols) != (length(brks) - 1)) {
            stop("Inconsistent colour levels / list of colours")
        }
    }
    if (vert) {
        par(mar = c(1, 1, 1, 1.5 * (1 + cex)), mgp = c(1, 1, 
            0), las = 1, cex = 1.2)
        image(1, c(1:length(cols)), t(c(1:length(cols))), axes = FALSE, 
            col = cols, xlab = "", ylab = "")
        box()
        axis(4, at = my.ticks, tick = TRUE, labels = my.labels, cex.axis = cex)
    }
    else {
        par(mar = c(0.5 + cex, 1, 1, 1), mgp = c(1.5, max(c(0.3, 
            0.8 * (cex - 0.625))), 0), las = 1, cex = 1.2)
        image(1:length(cols), 1, t(t(1:length(cols))), axes = FALSE, 
            col = cols, xlab = "", ylab = "")
        box()
        axis(1, at = my.ticks, labels = my.labels, cex.axis = cex)
    }
}
