用ggplot2画heatmap

> library(ggplot2)
Loading required package: proto
Loading required package: grid
Loading required package: reshape
Loading required package: plyr
Loading required package: digest
> sample.data <- matrix(rnorm(500), 50, 10)
> sample.data <- as.data.frame(sample.data)
> sample.data <- cbind(paste("g", 1:50, sep=""), sample.data)
> colnames(sample.data) <- c("GeneName", paste("t", 1:10, sep=""))
> head(sample.data)
  GeneName         t1         t2         t3         t4         t5         t6
1       g1  0.6506507  2.2511342 -0.3816986  0.3280272  0.4610605  1.5809543
2       g2 -0.6905213  0.3381060 -0.7077111  1.4078910  0.2933683 -0.1066459
3       g3  0.3541356  0.4541696 -1.6335950  0.4263433 -0.5189055  0.5252583
4       g4 -0.7536498 -0.6576679 -1.2011061  0.2127903 -0.2037233 -0.6236748
5       g5  0.1589113  0.4053087 -1.1849440 -0.2635171  2.0520325 -0.2831023
6       g6  0.4785667  0.2277287 -0.5114406  0.3719895  0.7284306  0.1884368
          t7         t8           t9        t10
1 -1.3000891  0.5892476  1.590302760 -0.3231108
2  2.0824780  0.9929615 -0.133183908  0.8461574
3  0.5556600 -1.2984789  0.000519446  0.3543165
4  1.5932854  0.3699189  0.972156471 -0.5041644
5  0.7586054  1.3547863  1.778556359  1.3227048
6 -0.1165529  1.2721110  1.082750129 -0.9139294
> sample.data <- melt(sample.data)
Using GeneName as id variables
> head(sample.data)
  GeneName variable      value
1       g1       t1  0.6506507
2       g2       t1 -0.6905213
3       g3       t1  0.3541356
4       g4       t1 -0.7536498
5       g5       t1  0.1589113
6       g6       t1  0.4785667
> p <- ggplot(sample.data, aes(variable, GeneName))
> p <- p + geom_tile(aes(fill = value),
+ colour = "white") + scale_fill_gradient(low="white", high="steelblue")
> p
> library(Cairo)
> CairoPNG("d:/heatmap0206.png", 800,800)
> p
> dev.off()
windows
      2

把variable和GeneName分别作为x轴和y轴,然后按着value的值去填充颜色。

Related Posts

Leave a comment

Your comment