Reputation: 141
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.
Upvotes: 4
Views: 11485
Reputation: 141
Thanks for the feedback, I will try it later. In the meantime, the following also did the trick:
filename <- "Data.txt"
test <- read.table(filename, sep ="\t", quote = "",
stringsAsFactors = FALSE,header = TRUE, row.names = 1)
input <- as.matrix(test)
heatmap.2(input, trace = "none", Colv=FALSE, dendogram = 'row',
density = "none", col = bluered(20),
cexRow = 0.6, cexCol = 0.9, margins = c(8,14), scale = "row",
hclust =function(x) hclust(x,method = "average"))
Upvotes: 1
Reputation: 106
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)
Upvotes: 2