heatmap data frame annotation issue
1
0
Entering edit mode
6.9 years ago
1769mkc ★ 1.2k

i can plot heat-map but i have issue with annotation i want to annotate rows and column so i have genes

Chemokines genes
CXCL1
CXCL2
CXCL3
CXCL5
CXCL6
CXCL8
CXCL10
CCl20
CCl22
CCl28

TNF related genes
TNFAIP2
TNFAIP3
TNFRSF11A
TNFRSF14
TNFRSF1B
TNFRSF25
TNFSF10
TNFSF13
TNFSF13B
TNFSF15
TNFSF9


INTERLEUKIN AND RELATED GENES
IL36G
IL17C
IL1B
IL1A
IL11RA
1L32
1L17RD
IL36RN
IL20RA
IL23A
IL4I1
IL2RG
IL15
1L20RB
IL15RA
IL17RC

library(gplots)
file1<- read.csv('data.csv',header = T)
class(file1)
dat <- data.frame(file1)
dim(dat)
names(dat)
head(dat)
rownames(dat) <-dat$gene
head(dat)
dim(dat)
head(dat)
dat.tdy <- dat[,2:7]
dat.n <- scale(t(dat.tdy))
dat.tn <- t(dat.n)
round(colMeans(dat.n),1)
apply(dat.n,2,sd)
d1 <- dist(dat.n,method = "euclidean", diag = FALSE, upper = FALSE)
d2 <- distdat.tn,method = "euclidean", diag = FALSE, upper = TRUE)
c1 <- hclust(d1, method = "complete", members = NULL)
c2 <- hclust(d2, method = "complete", members = NULL)



macolor = colorRampPalette(c("navyblue", "white", "red"))(100)



heatmap.2dat.tn,Rowv=as.dendrogram(c2),Colv = as.dendrogram(c1),col =macolor 
          ,scale="row", margin=c(6, 4),trace='none',
          symkey=FALSE,symbreaks=FALSE,dendrogram="both",  
          density.info='density', denscol="red",lhei=c(1,3),cexRow = 1,cexCol = 1,
          lwid=c(.9,3), keysize=0.1, key.par = list(cex=0.5))

The code above I have two condition control+salmonella and KD+salmonella ,

I want to annotate my row as such

     Chemokines genes  TNF related genes     INTERLEUKIN AND RELATED GENES

similarly i want to annotate my column as control+salmonella KD+salmonella ,

I saw the manual with the car data set but its confusing im not able to do it..

My issue is i have a create a annotation data frame for row specific genes as and the based on the condition which is column specific

Any suggestion or help would be highly appreciated.

R • 3.7k views
ADD COMMENT
5
Entering edit mode
6.9 years ago

For the gene colours, you can set it with the RowSideColors parameter to heatmap.2:

#Create vectors of the gene names of interest
chemokine <- c("CXCL1","CXCL2","CXCL3","CXCL5","CXCL6","CXCL8","CXCL10","CCl20","CCl22","CCl28")
TNF <- c("TNFAIP2","TNFAIP3","TNFRSF11A","TNFRSF14","TNFRSF1B","TNFRSF25","TNFSF10","TNFSF13","TNFSF13B","TNFSF15","TNFSF9")
interleukin <-c("IL36G","IL17C","IL1B","IL1A","IL11RA","1L32","1L17RD","IL36RN","IL20RA","IL23A","IL4I1","IL2RG","IL15","1L20RB","IL15RA","IL17RC")

#Create a new colour vector based on the rownames of dat.tn
coloursGenes <- rownames( dat.tn )

#Set colours
coloursGenes[which(coloursGenes %in% chemokine)] <- "forestgreen"
coloursGenes[which(coloursGenes %in% TNF)] <- "gold"
coloursGenes[which(coloursGenes %in% interleukin)] <- "royalblue"

#NB - if your data.tn contains genes other than these, you can set these other genes to another colour with:
#coloursGenes[which(coloursGenes != c(chemokine, TNF, interleukin))] <- "white"

heatmap.2(..., RowSideColors=coloursGenes, ...)

----------------------------------

For the ColSideColors, it would be beneficial to already have a vector that contains the sample-to-group assignment? If you already have such a vector, it would look like groups <- c("control+salmonella", "KD+salmonella", "control+salmonella", "control+salmonella", "KD+salmonella", "KD+salmonella", ...) and it's order should match the sample order in dat.tn.

To then set colours based on this sample-to-group assignment:

require("RColorBrewer")
coloursSamples <- factor(groups, levels=c("control+salmonella", "KD+salmonella"))
coloursSamples <- colorRampPalette(c("royalblue", "red3"))(length(unique(coloursSamples)))[factor(coloursSamples)]

heatmap.2(..., RowSideColors=coloursGenes, ColSideColors=coloursSamples...)

If you don't have such a vector already, then just create it yourself and ensure that it matches the ordering of samples in dat.tn.

Kevin

PS- aapologies about the mixing between British and American English (colour / color)

ADD COMMENT
0
Entering edit mode

thanks a ton as always ...is it possible to label those row and column annotation as text legend ...?because in the code you defined it but someone looking at it would find it difficult to interpret ,how to add the text or as make a legend to show the annotation ?

my heatmap

ADD REPLY
0
Entering edit mode

Yes, of course, you can add custom legends with:

#Vertical-stacked legend
legend("topright", bty="n", cex=0.8, title="Group", c("control+salmonella","KD+salmonella"), fill=c("sroyalblue","red3"), horiz=FALSE)

#Horizontal legend
legend("bottom", bty="n", cex=0.8, title="Group", c("control+salmonella","KD+salmonella"), fill=c("sroyalblue","red3"), horiz=TRUE)

For the position (first parameter), you can specify top, left, bottom, right, or X Y co-ordinates (2 parameters).

It may take a while to correctly position the legend. The positioning works in conjunction with:

  • mar parameter to par() (outside of heatmap.2)
  • margin parameter to heatmap.2
ADD REPLY
1
Entering edit mode

okay.. ..let me use it ...

ADD REPLY
1
Entering edit mode

My friend, I would additionally encourage you to take a look at ComplexHeatmap, where manipulating the heatmap annotations is much more flexible than other heatmap packages. I have posted some initial starter code here:

C: how to cluster genes in heatmap

ADD REPLY
0
Entering edit mode

well because my PI need heatmap with Z score so thats is the reason why im using heatmap.2

ADD REPLY
0
Entering edit mode

You can convert any data-matrix to Z-scores outside of the heatmap.2 function. If you do this, however, then you must set scale=FALSE inside the heatmap.2 funtion

ADD REPLY
0
Entering edit mode

okay...I think complex heatmap is really rich as compared to heatmap.2 and pheatmap so i can make the data matrix into Z score and then pass it on to the complex heatmap....that is what you are suggesting ?

ADD REPLY
0
Entering edit mode

i will use your code from the link you have given ...for sure...

ADD REPLY
0
Entering edit mode

i can do the same for row side annotation? isn't it?

ADD REPLY
1
Entering edit mode

Yes, you can technically create any legend that you want.

For example:

legend("bottom", bty="n", cex=0.8, title="Political party", c("Democrats","Republicans"), fill=c("royalblue","red3"), horiz=TRUE)

legend("bottom", bty="n", cex=0.8, title="Cell-type", c("chemokine","TNF","interleukin"), fill=c("forestgreen","gold","royalblue"), horiz=TRUE)
ADD REPLY
0
Entering edit mode

thank...you....very much...

ADD REPLY
2
Entering edit mode

Okay, make sure that you take some time off over Christmas (if you celebrate Christmas)

ADD REPLY
1
Entering edit mode

same to you, i mean every time i ask a question you are the one to come up with solution that's Christmas for me

ADD REPLY
1
Entering edit mode

Good to hear. All I can say is that hard work pays off

ADD REPLY

Login before adding your answer.

Traffic: 1496 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6