How to add both row and column labels to Complexheatmap
2
0
Entering edit mode
dilawerkh4 • 0
@dilawerkh4-14012
Last seen 7.2 years ago

I have a tab delimited text file with a header (sample metrics), and 1st column contains sample names, hypothetically something like this:

My actual table has 13 columns.

SAMPLE NAME EUROPEAN ASIAN AFRICAN
Grejoujkd 24 5 1
Ljkiuiuiuo 5 46 3
Mkjkueeyy 25 3 20

I would like the sample names to be row labels on the heatmap, and Metrics (European, Asian, African) to be column labels.

Using this simple script, the column labels got printed, but the row labels did not. Any suggestions on how to add the row labels (sample names) to the heatmap:

library(ComplexHeatmap)

filename <- "Data.txt"

# Read the data into a data frame
my_data <- read.table(filename, sep ="\t", quote = "", stringsAsFactors = FALSE,header = TRUE)

# Make the heatmap data into a matrix
my_matrix <- as.matrix(my_data[ ,c(2:13)]) # [all rows, columns 2-13]

# Make heatmap
Heatmap(my_matrix)

 

Here is my heatmap which shows that the row labels did not print

https://drive.google.com/file/d/0B-ObXiVfL-RzZWZfWW95S25oVjA/view?usp=sharing

complexheatmap labels • 9.5k views
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 1 day ago
United States

This is a basic R question, and would be better posed on say R-help (r-help@r-project.org), or better yet, by simply reading the help page for read.table. From ?read.table

row.names: a vector of row names.  This can be a vector giving the
          actual row names, or a single number giving the column of the
          table which contains the row names, or character string
          giving the name of the table column containing the row names.

          If there is a header and the first row contains one fewer
          field than the number of columns, the first column in the
          input is used for the row names.  Otherwise if 'row.names' is
          missing, the rows are numbered.

          Using 'row.names = NULL' forces row numbering. Missing or
          'NULL' 'row.names' generate row names that are considered to
          be 'automatic' (and not preserved by 'as.matrix').

 

ADD COMMENT
0
Entering edit mode

Thanks for the feedback. I added row.names, but for some reason the program Complexheatmap does not function properly when row.names are added. Works fine with heatmap.2 though.

Here is what I added. Everything else the same as above:

my_data <- read.table(filename, sep ="\t", quote = "",
                      stringsAsFactors = FALSE,header = TRUE, row.names=1)

 

Here is the error message I got:

> my_matrix <- as.matrix(my_data[ ,c(2:13)]) # [all rows, columns 2-13]
Error in `[.data.frame`(my_data, , c(2:13)) : undefined columns selected

 

When I changed this to :

my_matrix <- as.matrix(my_data)

the error message went away,  but now the heatmap is messed up.

 

I checked all the documentation and examples for Complexheatmap, and it seems there is no example or heatmap shown with BOTH row and column labels, so I am not sure if Complexheatmap has that functionality and can out output BOTH row and column labels simultaneously.

 

 

Everything plots great with heatmap.2, but I like complexheatmap because it is more versatile. However, complexheatmap seems to have a MAJOR HANDICAP in that you can't output BOTH row and column labels simultaneously.

 

Any feedback will be greatly appreciated.


 

ADD REPLY
1
Entering edit mode

I am not sure you checked all the documentation and examples for ComplexHeatmap because literally the very first example has both row and column names! From ?Heatmap:

     mat = matrix(rnorm(80, 2), 8, 10)
     mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
     rownames(mat) = letters[1:12]
     colnames(mat) = letters[1:10]
     
     require(circlize)
     
     Heatmap(mat)

In addition:

z <- matrix(c(24,5,25,5,46,3,1,3,20), ncol = 3, dimnames = list(c("Grej","Ljkiu","Mkjku"), c("EUR","ASN","AFR")))
> z
      EUR ASN AFR
Grej   24   5   1
Ljkiu   5  46   3
Mkjku  25   3  20

> Heatmap(z)

ADD REPLY
0
Entering edit mode

Thanks alot for the reply. The only problem with this is my data frame has 100 rows and 13 columns, so it would take forever to manually enter each vector value. Is there a way to feed the whole data frame without specifying each of the 1300 values in the matrix individually?

ADD REPLY
0
Entering edit mode
dilawerkh4 • 0
@dilawerkh4-14012
Last seen 7.2 years ago

Thanks James, problem SOLVED!

 

I revised the following from my original post above, changes are highlighted:

my_data <- read.table(filename, sep ="\t", quote = "",
                      stringsAsFactors = FALSE, header = TRUE,
                      row.names = 1)

 

# Make the heatmap data into a matrix
my_matrix <- as.matrix(my_data[ ,c(2:12)]) # [all rows, columns 2-12]

Heatmap(my_matrix, cluster_columns = FALSE,
        column_names_gp = gpar(cex=fontsize),
        row_names_gp = gpar(fontsize = 7),
        row_dend_width = unit(3, "cm"), show_row_names = TRUE )

 


 

ADD COMMENT

Login before adding your answer.

Traffic: 657 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

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

Powered by the version 2.3.6