Hello again, lately I have been playing a lot with heatmap functions, in particular I complemented heatmap.2 with a nice feature from heatmap.plus, i.e. the possibility of assigning a matrix to ColSideColors so to take into account more annotations. Below is the code I introduced in heatmap.2, everything else is maintained equal to heatmap.2:
if (!missing(ColSideColors)) { #if (!is.matrix(ColSideColors)) #stop("'ColSideColors' must be a matrix") if (!is.character(ColSideColors) || dim(ColSideColors)[1] != nc) stop("'ColSideColors' dim()[2] must be of length ncol(x)") lmat <- rbind(lmat[1, ] + 1, c(NA, 1), lmat[2, ] + 1) lhei <- c(lhei[1], 0.2, lhei[2]) } if (!missing(RowSideColors)) { #if (!is.matrix(RowSideColors)) #stop("'RowSideColors' must be a matrix") if (!is.character(RowSideColors) || dim(RowSideColors)[1] != nr) stop("'RowSideColors' must be a character vector of length nrow(x)") lmat <- cbind(lmat[, 1] + 1, c(rep(NA, nrow(lmat) - 1), 1), lmat[, 2] + 1) lwid <- c(lwid[1], 0.2, lwid[2]) } lmat[is.na(lmat)] <- 0 } if (length(lhei) != nrow(lmat)) stop("lhei must have length = nrow(lmat) = ", nrow(lmat)) if (length(lwid) != ncol(lmat)) stop("lwid must have length = ncol(lmat) =", ncol(lmat)) op <- par(no.readonly = TRUE) on.exit(par(op)) layout(lmat, widths = lwid, heights = lhei, respect = FALSE) if (!missing(RowSideColors)) { if (!is.matrix(RowSideColors)){ par(mar = c(margins[1], 0, 0, 0.5)) image(rbind(1:nr), col = RowSideColors[rowInd], axes = FALSE) } else{ par(mar = c(margins[1], 0, 0, 0.5)) rsc = RowSideColors[rowInd, ] rsc.colors = matrix() rsc.names = names(table(rsc)) rsc.i = 1 for (rsc.name in rsc.names) { rsc.colors[rsc.i] = rsc.name rsc[rsc == rsc.name] = rsc.i rsc.i = rsc.i + 1 } rsc = matrix(as.numeric(rsc), nrow = dim(rsc)[1]) image(t(rsc), col = as.vector(rsc.colors), axes = FALSE) if (length(colnames(RowSideColors)) > 0) { axis(1, 0:(dim(rsc)[2] - 1)/(dim(rsc)[2] - 1), colnames(RowSideColors), las = 2, tick = FALSE) } } } if (!missing(ColSideColors)) { if (!is.matrix(ColSideColors)){ par(mar = c(0.5, 0, 0, margins[2])) image(cbind(1:nc), col = ColSideColors[colInd], axes = FALSE) } else{ par(mar = c(0.5, 0, 0, margins[2])) csc = ColSideColors[colInd, ] csc.colors = matrix() csc.names = names(table(csc)) csc.i = 1 for (csc.name in csc.names) { csc.colors[csc.i] = csc.name csc[csc == csc.name] = csc.i csc.i = csc.i + 1 } csc = matrix(as.numeric(csc), nrow = dim(csc)[1]) image(csc, col = as.vector(csc.colors), axes = FALSE) if (length(colnames(ColSideColors)) > 0) { axis(2, 0:(dim(csc)[2] - 1)/(dim(csc)[2] - 1), colnames(ColSideColors), las = 2, tick = FALSE) } } } What is weird is the fact that the dimension of histogram plotted above the heatmap, accounting for the annotations, is always the same (fixed!), so when you have 3 or more annotations, the same length of the bars is divided by 3 or more, making the reading of the figure impossibile.. Does anyone know how to fix this? Thanks! g -- View this message in context: http://n4.nabble.com/heatmap-3-tp1566584p1566584.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.