Pheatmap row annotation multiple categories
2
0
Entering edit mode
2.9 years ago

Hello,

I know it is possible to generate a heatmap with row annotations using pheatmap, but is it possible to generate it when a row falls into 2 categories?

For example, in the code bellow, how can I add the side bar showing that tomato is both a fruit and used in a salad?

library(pheatmap)

x = matrix(1:16, nrow = 4,
dimnames = list(c("banana","lime","carrot","tomato"), c("A1","A2","B1","B2")))
x

col_ann = data.frame(cbind(c("A1","A2","B1","B2"),c("A","A","B","B")))
colnames(col_ann)=c("sample","category1")
rownames(col_ann)=col_ann$sample col_ann$sample<-NULL

row_ann = data.frame(cbind(c("banana","lime","carrot","tomato","tomato"),
colnames(row_ann)=c("food","category2")

ann_color = list("category1"=c("A"="yellow","B"="blue"),

pheatmap(x,
cluster_rows = T,show_rownames = T, show_colnames=F,
border_color=NA, scale='row',
annotation_col = col_ann,
annotation_row = row_ann,
annotation_colors = ann_color )

pheatmap row annotation R • 4.3k views
1
Entering edit mode

Just create a separate category and colour for those that are both a fruit and salad? Otherwise, you may have to draw out how exactly you are visualising this should appear in your mind.

3
Entering edit mode
2.9 years ago

There's two ways I can see you do this. An easy way is to add just a "both" value (as I see now Kevin suggested in the comment.). In this case it makes sense since there's only two choices.

Or if you really want to (or will have cases where "both" doesn't cut it because there's more categories), make Salad and Fruit binary annotations and encode "Yes" or "No" in each vector.

Code:

### Option one ####
library(pheatmap)

x = matrix(1:16, nrow = 4,
dimnames = list(c("banana","lime","carrot","tomato"), c("A1","A2","B1","B2")))
x

col_ann = data.frame(cbind(c("A1","A2","B1","B2"),c("A","A","B","B")))
colnames(col_ann)=c("sample","category1")
rownames(col_ann)=col_ann$sample col_ann$sample<-NULL

rownames(row_ann) <- c("banana","lime","carrot","tomato")

colnames(row_ann)=c("category2")

ann_color = list("category1"=c("A"="yellow","B"="blue"),

pheatmap(x,
cluster_rows = T,show_rownames = T, show_colnames=F,
border_color=NA, scale='row',
annotation_col = col_ann,
annotation_row = row_ann,
annotation_colors = ann_color )

### Option two ####

row_ann = data.frame(Fruit=c("Yes","No","No","Yes"),
rownames(row_ann) <- c("banana","lime","carrot","tomato")

ann_color = list("Fruit"=c("Yes"="red","No"="white"),

pheatmap(x,
cluster_rows = T,show_rownames = T, show_colnames=F,
border_color=NA, scale='row',
annotation_col = col_ann,
annotation_row = row_ann,
annotation_colors = ann_color )

1
Entering edit mode
2.9 years ago
Mark ★ 1.1k

If you add another column/variable into the row_ann then it will be annotated. eg:

row_ann\$vegetable <- c("Veg", "Not", "Veg", "Veg", "Veg")


I can't test your code because pheatmap just isn't working for me. Not sure why...

0
Entering edit mode

The code did not run for me, either.