Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heatmap.2 row labels don't show on heatmap

Tags:

r

heatmap

I have a tab separated data text file (Data.txt) with 13 columns and 90 rows. It has a header row (sample values) and the 1st column is sample names.

When I load my data into a data matrix my 1st column sample names don't show. They are all replaced with NA. I believe this causes the sample names not to show up as column labels on the heatmap.

Here is my simple script:

library(gplots)
y <- data.matrix(Data)
heatmap.2(y, main=K12_Ancient_Calculator, trace="none", 
          margins = c(10,12), cexRow=0.5)

Any ideas what needs to be done so that the sample names in the 1st column of my data file show up in y (data matrix). Currently they show up as NA.

I have noticed that in my version of Rstudio there is no option to declare 1st column of data file as "labels" when using the "import dataset" feature.

All feedback greatly appreciated.

like image 651
Gene100 Avatar asked Oct 30 '25 16:10

Gene100


1 Answers

heatmap.2 requires a matrix as input which only accepts numeric values (or NA) My guess is that your sample names is a character vector, which will be converted to NA by data.matrix() (NAs introduced by coercion)

Try this:

y <- data.matrix(Data)
row.names(y) <- Data[,1] # Set rownames
y <- y[,-1] # Remove column with NA

Full example below using the labRow argument of heatmap.2. The default of labRow is rownames of your data matrix. These are likely empty in your case. They can be set using one of the options below

library(gplots)
library(dplyr)

y <- iris %>% 
  sample_frac(0.3) # Take a subset

y.mat <- y %>% 
  select(-Species) %>% ## Remove identifier
  data.matrix() ## Convert to matrix

heatmap.2(y.mat, main="iris", trace="none", 
          margins = c(10,12), cexRow=0.5,
          labRow = y$Species)

## Alternatively set the rownames of your data.matrix
y.mat2 <- y.mat
row.names(y.mat2) <- y$Species
heatmap.2(y.mat2, main="iris", trace="none", 
          margins = c(10,12), cexRow=0.5)
like image 179
dmu Avatar answered Nov 02 '25 08:11

dmu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!