Entering edit mode
3.1 years ago
foxiw
▴
10
Hi,
I'm trying to plot sample distances via a heatmap, and I'd like to add three categorical variables to the plot: Litter, sex, and genotype. I'd like the litter and sex annotations as column annotations, and the genotype as the row annotations. However, I can only add the column annotations for some reason. I've checked to see whether all rows for the dataframes match, and they do. Not sure what else to do. Code is below:
vsd <- vst(dds, blind = TRUE)
head(assay(vsd), 3)
#Create sample distances by calculating the Eucidian distance between the samples using the vst dataset.
#t in dist function means 'transpose' since the dist function requires the samples be in rows, not columns
sampleDist <- dist(t(assay(vsd)))
sampleDist
#Convert sampleDist into a matrix (requirement for pheatmap), assign all of the annotations etc. then create
#heatmap
sampleDistMatrix <- as.matrix( sampleDist )
rownames(sampleDistMatrix) <- vsd$Genotype
colnames(sampleDistMatrix) <- vsd$Sample_ID
colors <- colorRampPalette( rev(brewer.pal(9, "Blues")))(255)
#Annotations
anno_colors <- list(Sex = c(Female = "#E41A1C", Male = "#4A72A6"),
Genotype = c(WTMale = "#02818A", WTFemale = "#67A9CF", HET = "#c712c1", KO = "#7312c7"),
Litter = c(Litter1 = "#000000", Litter2 = "#686868", Litter3 = "#C6C6C6", Litter4 = "#FFFFFF"))
Annotation_col <- data.frame(
rownames = factor(metadata$Sample_ID, ordered = TRUE),
Litter = factor(metadata$Litter, ordered = TRUE),
Sex = factor(metadata$Sex, ordered = TRUE))
rownames(Annotation_col) <- Annotation_col[,1]
Annotation_col <- Annotation_col[,-1]
Annotation_row <- data.frame(
rownames = factor(metadata$Sample_ID, ordered = TRUE),
Genotype = factor(metadata$Genotype, ordered = TRUE))
rownames(Annotation_row) <- Annotation_row[,1]
Annotation_row <- Annotation_row[,-1]
#Heatmap
p1 <- pheatmap(sampleDistMatrix,
clustering_distance_rows = sampleDist,
clustering_distance_cols = sampleDist,
show_rownames = FALSE,
annotation_col = Annotation_col,
annotation_row = Annotation_row,
annotation_colors = anno_colors,
col = colors)
p1
Here is the plot:
And this is the error it produces if I try and add the row annotations:
Error in `[.default`(annotation_row, rownames(mat), , drop = F) :
incorrect number of dimensions
Any help would be much appreciated!
SampleDistMatrix is a symmetrical matrix, yet you are using the different names:
Then again in Annotation_row you are using Sample_ID as the row names. I would suggest you could use
too. Btw, you can specify the row names directly in the call of data.frame by row.names=...
I've managed to figure it out by more or less doing what you've said. Thank you for the row.names = hint! Sorry if this was a stupid question. As you can tell, I am new to RNA-seq analysis haha