This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Help with adding a row color side bar using pheatmap??

Hi All,

I'm trying to figure out the best way to associate a color side bar to different factors for the rows in my data set when using pheatmap.

Here is a code with a dummy data set to illustrate what I want:

require(pheatmap)
# dummy data
dummymat = matrix(rnorm(100), 10, 10)
colnames(dummymat) = paste("Patient", 1:10, sep = "")
rownames(dummymat) = paste("Gene", 1:10, sep = "")

# create a data frame with the patients categories
categories <- data.frame(Sex = factor(sample(c("Male", "Female"),size = 10,replace = T), labels = c("Male", "Female")),
Stage= factor(sample(c('I','II','III'),size = 10,replace = T), labels = c('I','II','III')))
rownames(categories) <- colnames(dummymat) 

categoriesrow <- data.frame(TDC = factor(sample(c("BB", "DD"),size = 10,replace = T), labels = c("BB", "DD")), Bcells= factor(sample(c('WW','RR','TT'),size = 10,replace = T), labels = c('WW','RR','TT')))
colnames(categoriesrow) <- rownames(dummymat) 

pheatmap(dummymat, annotation_col = categories, annotation_row = categoriesrow)

With the code above I'm able to create a very nice column color bar (if I take out the "annotation_row = categoriesrow"), but once I try to create a row color bar I get errors. The error that occurs is after I process "colnames(categories) <- rownames(dummymat)"

Error in colnames<-(*tmp*, value = c("Gene1", "Gene2", "Gene3", "Gene4", : 'names' attribute [10] must be the same length as the vector [2]

Can someone please let me know the best way to achieve what I want? I would appreciate any help!

Thanks, Afshin

r software error

2 answers

I executed your script and dimensions of your categoriesrow are 10X2 and you cannot specify 10 column names to a dataframe with 2 columns.

Change this:

colnames(categoriesrow) <- rownames(dummymat)

to:

rownames(categoriesrow) <- rownames(dummymat)

Would work.

The problem that I can see with your code is that categoriesrow is a data frame with 2 columns, named TDC and Bcells.

The line

colnames(categoriesrow) <- rownames(dummymat)

will give an error, because dummymat has 10 rows, the gene names.

Presumably what you want is

rownames(categoriesrow) <- rownames(dummymat)

Log in to answer this question.