ColSideColors set by rows for heatmap.2 in RStudio
Entering edit mode
Last seen 5.9 years ago

I can't figure out to make a condition_colors for ColSideColors with Heatmap.2 in RStudio.

I have a matrix in csv file with 49 genes as rows and 11 samples, I want to group the genes by biological process. The first 8 genes I want one color, the next 10 genes another, an so forth like below. 

# Set column side colors
condition_colors <- unlist(lapply(row.names(Data),function(x){
  if(grepl([1:8,],x)) '#FF0000'
  else if(grepl([9:19,]),x)) '#FF7F7F'
  else if(grepl([20:27,],x)) '#0000FF'
  else if (grepl([28:49,],x)) '#8080FF'

This does not work, how does one continue?

rstudio heatmap.2 • 4.0k views
Entering edit mode
Last seen 7 days ago
United States

This function comes from gplots, a CRAN package, so you should be asking this question on R-help ( That said, from ?heatmap.2, there is this:

ColSideColors: (optional) character vector of length 'ncol(x)'
          containing the color names for a horizontal side bar that may
          be used to annotate the columns of 'x'.

RowSideColors: (optional) character vector of length 'nrow(x)'
          containing the color names for a vertical side bar that may
          be used to annotate the rows of 'x'.

Using ColSideColors to annotate your genes (which are in rows) seems not the way to proceed.

In addition that if/else stack is sort of the hard way to do things. Wouldn't

condition_colors <- rep(c('#FF0000','#FF7F7F','#0000FF','#8080FF'), c(8,11,8,22))

be a simpler way to do that? Also note that 9:19 has 11 items, not 10, so your counting might be off as well.

Entering edit mode

Thank you very much, it worked! I am new to R/RStudio and will take all of your comments into consideration. 


Login before adding your answer.

Traffic: 793 users visited in the last hour
Help About
Access RSS

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6