0

I have to plot a heatmap of some gene expression values. In other words each row of the heatmap represents the mean of 10 genes so that:

  • row1 --> mean_genes[1:10],
  • row2 --> mean_genes[11:20],
  • row3 --> mean_genes[21:30].

Up to here, all is ok, because of I know the function heatmap. But then, on the heatmap I have to plot the distribution (with a line) row by row of the expression value of 1 gene at a time:

  • row1 --> gene_expression[1],
  • row2 --> gene_expression[11],
  • row3 --> gene_expression[31].

and so on for all the rows because of finally I would like to check the expression values of the groups of 10 genes row by row and also the expression value of the gene of the interest to perform a comparison (by eyes). However I have totally no idea on how to produce such complex plot.

Any suggestions about it?

Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
Fuv8
  • 885
  • 3
  • 11
  • 21
  • 3
    Please make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Thomas Nov 14 '13 at 22:42
  • Yes I know but it is only an idea..I don't know how to make reproducible examples. – Fuv8 Nov 14 '13 at 23:02
  • Well, supply some example data and show what functions you have in mind, then explain why what you've tried isn't producing your intended result. It is hard to give any kind of meaningful answer without something to work from. – Thomas Nov 14 '13 at 23:06
  • Check out the `heatmap.2` function in the `gplot` package. – Scott Ritchie Nov 15 '13 at 03:48

1 Answers1

1

It's a bit hard to be sure what you're after without example data, but maybe something using ggplot2 and a geom_tile() plot as follows (apologies it's not prettied up but you can fiddle with it):

## LIBRARY (must be installed)
require(ggplot2)
require(plyr)

## CREATE THE GENEMAP DATA
geneMap<-data.frame(1:100,expand.grid(1:10,1:10),rnorm(100,6,2))
colnames(geneMap)<-c("ID","X","Y","expr")

## APPEND THE ROWMEANS TO EACH ITEM
rowMean<-ddply(geneMap, "X" ,function(df)mean(df$expr))
geneMap<-merge(geneMap,rowMean,by="X")
colnames(geneMap)<-c("X","ID","Y","expr","rMean")

## CREATE A BASIC TILE WITH FILLED ROWS (FILL BY MEAN, ALPHA BY VALUE)
hmap<-ggplot(data=geneMap, aes(x=X,y=Y)) +        # Basic Plot
      theme_bw() +                                # Basic Theme
      geom_tile(aes(fill=rMean)) +                # Fill the tile by mean
      scale_x_continuous( breaks = 1:10,1) +      # Force ticks 1-10 on x axis
      scale_y_continuous( breaks = 1:10,1) +      # Force ticks 1-10 ony axis
      scale_fill_gradient(low="yellow", high="orange")   # Color the heatmap

hmap <- hmap + annotate("text", x = geneMap$X, y = geneMap$Y, 
                        label = round(geneMap$expr,2))  # Label each point with value

meanSummary<-unique(geneMap[,c("X","rMean")])     # Pull out the means for each row
origSummary<-geneMap[geneMap$Y==1,]               # Pull out the original "seed" vals for each row

hmap<- hmap + annotate("text", x = meanSummary$X, 
                       y = max(geneMap$Y)+1, 
                       label = round(meanSummary$rMean,2)) # Add the mean labels at row end
hmap<- hmap + annotate("text", x = min(geneMap$Y)-1,
                       y = max(geneMap$Y)+1, label = "MEANS") # Label the row

hmap<- hmap + geom_line(aes(x=origSummary$X, 
                            y=origSummary$expr*(max(origSummary$X)/max(origSummary$expr)),
                            size=3, alpha=0.6))   # Add the line plot

# Draw the map & flip is so the rows run horizontally (or not!)
hmap + coord_flip()
Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64
Troy
  • 8,581
  • 29
  • 32
  • Hi Troy! It works very well and fits my question. You have just to add: require(plyr). ;-) – Fuv8 Nov 15 '13 at 15:33