Dear all, I was wondering if you could help me to construct a heat map, in which the columns are sorted by sample type (A first and then B). My reproducible example below runs, but the columns of the heatmap are not organized in the way I would like because it has first sampleA, SampleB, sampleA, and then sampleB. A = matrix(rnorm(20), 5,8) A colnames(A) <- c ("Sample1.A", "Sample2.B", "Sample3.A", "Sample4.B", "Sample5.A", "Sample6.B", "Sample7.A", "Sample8.B") A rownames(A) <- c ("protein1","protein2", "protein3", "protein4","protein5") A heatmap.2(A, dendrogram="col", Rowv = colnames(A), tracecol NA,col=bluered(64), sub = "", distfun = function(y) dist(y, method = "euclidean"), hclustfun function(y) hclust(y, method = "median"), scale = "row",cexCol = 1, cexRow = 0.9, ylab = "Enzymes", xlab "Samples", margins = c(9,9), keysize = 1, main='Test') ?Thanks. -- Andre [[alternative HTML version deleted]]
Hi, you have a couple of things going on here. You can reorder your matrix by creating an index like this:> A_index=c(grep(".A", colnames(A)), grep(".B", colnames(A)))and do this:> heatmap.2(A[,A_index], dendrogram="col", Rowv = colnames(A)[A_index], tracecol NA,col=bluered(64), sub = "",distfun = function(y) dist(y, method = "euclidean"), hclustfun function(y) hclust(y, method = "median"), scale = "row",cexCol = 1, cexRow = 0.9, ylab = "Enzymes", xlab "Samples", margins = c(9,9), keysize = 1, main='Test') But you're constructing your dendrogram on the columns, so your columns get reordered by mean weight. (Also, you've misunderstood Rowv and Colv.) However, if you force your columns to not reorder by setting "Colv=FALSE", AND tell it to construct the dendrogram on the columns, you get a warning, because that's contradictory:> heatmap.2(A[,A_index], dendrogram="col", Colv = FALSE, tracecol = NA,col=bluered(64), sub = "",distfun = function(y) dist(y, method = "euclidean"), hclustfun = function(y) hclust(y, method = "median"), scale = "row",cexCol = 1, cexRow = 0.9, ylab = "Enzymes", xlab "Samples", margins = c(9,9), keysize = 1, main='Test') Warning message: In heatmap.2(A[, A_index], dendrogram = "col", Colv = FALSE, tracecol = NA, : Discrepancy: Colv is FALSE, while dendrogram is `column'. Omitting column dendogram. Maybe you really meant to build your dendrogram on the rows, like this: heatmap.2(A[,A_index], dendrogram="row", Colv = FALSE, tracecol = NA,col=bluered(64), sub = "", distfun = function(y) dist(y, method = "euclidean"), hclustfun = function(y) hclust(y, method = "median"), scale = "row",cexCol = 1, cexRow = 0.9, ylab = "Enzymes", xlab "Samples", margins = c(9,9), keysize = 1, main='Test') If that's not it, I suggest playing around with the examples in ?heatmap.2 since the documentation is kind of opaque, but the examples are pretty good.>>>>> "ALN" == Andr? Luis Neves <andrluis at ualberta.ca> writes:ALN> Dear all, ALN> I was wondering if you could help me to construct a heat map, in which the ALN> columns are sorted by sample type (A first and then B). ALN> My reproducible example below runs, but the columns of the heatmap are not ALN> organized in the way I would like because it has first sampleA, SampleB, ALN> sampleA, and then sampleB. ALN> A = matrix(rnorm(20), 5,8) ALN> A ALN> colnames(A) <- c ("Sample1.A", "Sample2.B", "Sample3.A", "Sample4.B", ALN> "Sample5.A", "Sample6.B", "Sample7.A", "Sample8.B") ALN> A ALN> rownames(A) <- c ("protein1","protein2", "protein3", "protein4","protein5") ALN> A ALN> heatmap.2(A, dendrogram="col", Rowv = colnames(A), tracecol ALN> NA,col=bluered(64), sub = "", ALN> distfun = function(y) dist(y, method = "euclidean"), hclustfun ALN> function(y) hclust(y, method = "median"), ALN> scale = "row",cexCol = 1, cexRow = 0.9, ylab = "Enzymes", xlab ALN> "Samples", margins = c(9,9), keysize = 1, ALN> main='Test') ALN> ?Thanks. ALN> -- ALN> Andre ALN> [[alternative HTML version deleted]] ALN> ______________________________________________ ALN> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see ALN> https://stat.ethz.ch/mailman/listinfo/r-help ALN> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html ALN> and provide commented, minimal, self-contained, reproducible code. -- Patricia J. Hawkins
Thanks, Patricia! I will do it by the rows, and your explanations were great and easy to follow! Thank you very much for your help and time. Andre On Mar 4, 2017 4:18 PM, "Patricia J. Hawkins" <phawkins at connact.com> wrote: Hi, you have a couple of things going on here. You can reorder your matrix by creating an index like this:> A_index=c(grep(".A", colnames(A)), grep(".B", colnames(A)))and do this:> heatmap.2(A[,A_index], dendrogram="col", Rowv = colnames(A)[A_index],tracecol NA,col=bluered(64), sub = "", distfun = function(y) dist(y, method = "euclidean"), hclustfun function(y) hclust(y, method = "median"), scale = "row",cexCol = 1, cexRow = 0.9, ylab = "Enzymes", xlab "Samples", margins = c(9,9), keysize = 1, main='Test') But you're constructing your dendrogram on the columns, so your columns get reordered by mean weight. (Also, you've misunderstood Rowv and Colv.) However, if you force your columns to not reorder by setting "Colv=FALSE", AND tell it to construct the dendrogram on the columns, you get a warning, because that's contradictory:> heatmap.2(A[,A_index], dendrogram="col", Colv = FALSE, tracecol NA,col=bluered(64), sub = "",distfun = function(y) dist(y, method = "euclidean"), hclustfun = function(y) hclust(y, method = "median"), scale = "row",cexCol = 1, cexRow = 0.9, ylab = "Enzymes", xlab "Samples", margins = c(9,9), keysize = 1, main='Test') Warning message: In heatmap.2(A[, A_index], dendrogram = "col", Colv = FALSE, tracecol NA, : Discrepancy: Colv is FALSE, while dendrogram is `column'. Omitting column dendogram. Maybe you really meant to build your dendrogram on the rows, like this: heatmap.2(A[,A_index], dendrogram="row", Colv = FALSE, tracecol NA,col=bluered(64), sub = "", distfun = function(y) dist(y, method = "euclidean"), hclustfun = function(y) hclust(y, method = "median"), scale = "row",cexCol = 1, cexRow = 0.9, ylab = "Enzymes", xlab "Samples", margins = c(9,9), keysize = 1, main='Test') If that's not it, I suggest playing around with the examples in ?heatmap.2 since the documentation is kind of opaque, but the examples are pretty good.>>>>> "ALN" == Andr? Luis Neves <andrluis at ualberta.ca> writes:ALN> Dear all, ALN> I was wondering if you could help me to construct a heat map, in which the ALN> columns are sorted by sample type (A first and then B). ALN> My reproducible example below runs, but the columns of the heatmap are not ALN> organized in the way I would like because it has first sampleA, SampleB, ALN> sampleA, and then sampleB. ALN> A = matrix(rnorm(20), 5,8) ALN> A ALN> colnames(A) <- c ("Sample1.A", "Sample2.B", "Sample3.A", "Sample4.B", ALN> "Sample5.A", "Sample6.B", "Sample7.A", "Sample8.B") ALN> A ALN> rownames(A) <- c ("protein1","protein2", "protein3", "protein4","protein5") ALN> A ALN> heatmap.2(A, dendrogram="col", Rowv = colnames(A), tracecol ALN> NA,col=bluered(64), sub = "", ALN> distfun = function(y) dist(y, method = "euclidean"), hclustfun ALN> function(y) hclust(y, method = "median"), ALN> scale = "row",cexCol = 1, cexRow = 0.9, ylab = "Enzymes", xlab ALN> "Samples", margins = c(9,9), keysize = 1, ALN> main='Test') ALN> ?Thanks. ALN> -- ALN> Andre ALN> [[alternative HTML version deleted]] ALN> ______________________________________________ ALN> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see ALN> https://stat.ethz.ch/mailman/listinfo/r-help ALN> PLEASE do read the posting guide http://www.R-project.org/ posting-guide.html ALN> and provide commented, minimal, self-contained, reproducible code. -- Patricia J. Hawkins [[alternative HTML version deleted]]