glad you figured this out... I had the same problem... did not realize that the rownames of the column_annotations had to match with the colnames of the matrix.... I thought it was positional until I found this post.
Hello,
I am using pheatmap to map RNA-seq data. I have run into the same problem as other users, in attempting to annotate my heatmaps. Unfortunately I'm in the early stages of learning R, so I am struggling to interpret the solutions offered in other posts.
I would like to annotate my heatmap by rows (genes falling into a particular category "Genetype", as well as columns "SubcloneCohort". I get the common error message:
Error in check.length("fill") : 'gpar' element 'fill' must not be length 0
If anyone can help me understand what is amiss I'll be very grateful! Here is a link to the .CSV data file
Thanks, Oliver
The code I am using is as follows:
Read in data
data <- read.csv("DNA repair genes.csv", comment.char="#")
assign labels in column 1 to "rnames"
rnames <- data[,1]
assign labels in column 67 to "gtype"
gtype <- data[,67]
transform column 2-66 into a matrix
matrix <- data.matrix(data[,2:66])
rownames(matrix) <- rnames
Generate annotations for columns
annotation_col = data.frame( SubcloneCohort = factor(rep(c("J", "O", "P", "K", "L", "N", "Q"), c(8, 10, 9, 10, 10, 7, 11))))
rownames(annotation_col) = paste("Cohort", 1:65, sep = "")
Generate annotations for rows
annotation_row = data.frame( GeneType = factor(rep(c("Strand break joining", "Base excision repair", "Poly polymerases", "Direct reversal of damage", "Repair of topoisomerase crosslinks", "Mismatch excision repair", "Homologour recombination", "Fanconi anaemia", "Non-homologous end joining"), c(6, 11, 3, 3, 2, 9, 19, 14, 7))))
rownames(annotation_row) = paste("Gene", 1:74, sep = "")
Display column annotations
pheatmap(matrix, annotation_col = annotation_col)
Draw heatmaps
pheatmap(matrix, cluster_rows=T, cluster_cols=F, scale = "row", clustering_distance_rows = "euclidean", gaps_col = c(8, 18, 27, 37, 47, 54), border_color = FALSE, color = colorRampPalette(c("red", "black", "green"))(499), #add this for special color pallette cellwidth = 8, cellheight = 10, main = "DNA repair", fontsize = 12, fontsize_row = 8, fontsize_col = 7)
1 answer
Solved! Updated code below:
#################################
Designate column and row titles
<h6>#</h6>rnames <- data[,1] # assign labels in column 1 to "rnames" cnames <- data[1:65, 68] # assign labels in column 68 to "cnames"
<h6>#</h6>Create matrix from data
<h6>#</h6>matrix <- data.matrix(data[, 2:66]) # transform column 2-67 into a matrix rownames(matrix) <- rnames colnames(matrix) <- cnames
<h6>#</h6>Generate annotations for columns
<h6>#</h6>column_annotations = data.frame( SubcloneCohort = factor(rep(c("MShef4 normal", "MShef11 normal O", "MShef11 normal P", "MShef11 +Y27632 K", "MShef11 +Y27632 L", "MShef11 5% oxygen N", "MShef11 5% oxygen Q"), c(8, 10, 9, 10, 10, 7, 11))))
rownames(column_annotations) = colnames(matrix)
<h6>#</h6>Generate annotations for rows
<h6>#</h6>row_annotations = data.frame( GeneType = factor(rep(c("Strand break joining", "Base excision repair", "Poly polymerases", "Direct reversal of damage", "Repair of topoisomerase crosslinks", "Mismatch excision repair", "Homologour recombination", "Fanconi anaemia", "Non-homologous end joining"), c(6, 11, 3, 3, 2, 9, 19, 14, 7))))
rownames(row_annotations) = rownames(matrix)
<h6>#</h6>Pheatmap drawing of heatmap
<h6>#</h6>Draw heatmaps with annotation, fix cell sizes, and save to file with correct size
pheatmap(matrix, cluster_rows=T, cluster_cols=F, scale = "row", clustering_distance_rows = "correlation", annotation_row = row_annotations, annotation_col = column_annotations, annotation_legend = TRUE, gaps_col = c(8, 18, 27, 37, 47, 54), border_color = FALSE, color = colorRampPalette(c("red", "black", "green"))(499), #add this for special color pallette cellwidth = 6, cellheight = 6, main = "DNA repair genes", fontsize = 8, fontsize_row = 6, fontsize_col = 7)
dev.off()
Log in to answer this question.