Question: heatmap data frame annotation issue
0
gravatar for krushnach80
24 months ago by
krushnach80630
krushnach80630 wrote:

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 • 1.5k views
ADD COMMENTlink modified 24 months ago by Kevin Blighe52k • written 24 months ago by krushnach80630
5
gravatar for Kevin Blighe
24 months ago by
Kevin Blighe52k
Kevin Blighe52k wrote:

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 COMMENTlink modified 24 months ago • written 24 months ago by Kevin Blighe52k

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 REPLYlink modified 24 months ago • written 24 months ago by krushnach80630

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 REPLYlink written 24 months ago by Kevin Blighe52k
1

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

ADD REPLYlink written 24 months ago by krushnach80630
1

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 REPLYlink written 24 months ago by Kevin Blighe52k

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

ADD REPLYlink written 24 months ago by krushnach80630

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 REPLYlink written 24 months ago by Kevin Blighe52k

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 REPLYlink written 24 months ago by krushnach80630

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

ADD REPLYlink written 24 months ago by krushnach80630

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

ADD REPLYlink written 24 months ago by krushnach80630
1

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 REPLYlink modified 24 months ago • written 24 months ago by Kevin Blighe52k

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

ADD REPLYlink written 23 months ago by krushnach80630
2

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

ADD REPLYlink written 23 months ago by Kevin Blighe52k
1

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 REPLYlink written 23 months ago by krushnach80630
1

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

ADD REPLYlink written 23 months ago by Kevin Blighe52k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1829 users visited in the last hour