搜索
查看: 1638|回复: 0

使用ggplot2绘制聚类热图

[复制链接]

2

主题

34

帖子

771

积分

高级会员

Rank: 4

积分
771
发表于 2018-2-7 23:11:39 | 显示全部楼层 |阅读模式
本帖最后由 x2yline 于 2018-2-7 23:17 编辑

所需包为:ggdendro、reshape2、ggplot2和cowplot
作图效果如下:


作图思路是将ggplot绘制的热图和ggdendro绘制的聚类图使用cowplot包进行合并

代码如下:
[Ruby] 纯文本查看 复制代码
rm(list=ls())
## prepare the test data
set.seed(666)
test_data <- as.data.frame(matrix(c(sample(100:20000, 40),
                                    sample(1000:25000, 40)), ncol=4))
colnames(test_data) <- c("normal 1", "normal 2", "tumor 1", "tumor 2")
test_data$miRNA = paste(
  paste("hsa-miR-", sample(1:300, 20), sep = ""), 
  sample(c("-3p", "-5p"), replace=TRUE, size=20), sep="")
rownames(test_data) <- test_data$miRNA
head(test_data)
test_data_matrix <- test_data[, c("tumor 1", "normal 1", "tumor 2", "normal 2")]

## clustering
library("ggdendro")
cluster.dendro <- as.dendrogram(
  hclust(d=dist(x=test_data_matrix))
  )

## Create dendro
library(ggplot2)
dendro.plot <- ggdendrogram(data = cluster.dendro, rotate = TRUE)
dendro.plot <- dendro.plot +
  theme(legend.position = "none")
### Preview the plot
print(dendro.plot)


## rerank the miRNAs of test_data according to the clustering result
cluster.order <- order.dendrogram(cluster.dendro)
test_data$miRNA <- factor(test_data$miRNA, levels=test_data$miRNA[cluster.order],
                          ordered=TRUE)

## transform test_data to a long format dataframe
library(reshape2)
test_data_long <- melt(test_data, id.vars=c("miRNA"), 
                       variable.name="sample",
                       value.name="normlized_count")
test_data_long$sample <- factor(test_data_long$sample,
                                levels=c("normal 1", "tumor 1", "normal 2", "tumor 2"))
head(test_data_long)

## plot heatmap using ggplot

p <- ggplot(test_data_long, aes(sample, miRNA))+
  geom_tile(aes(fill=normlized_count), colour="white")+
  scale_fill_gradientn(values=c(0, 0.5, 1), 
                       colours=c("white", "black", "red"))

### change the axis and background
p <- p + theme_gray(8) +
  theme(legend.position = "none", axis.ticks = element_blank(),
        axis.text.x = element_text(colour = "grey20", size=10),
        axis.text.y = element_text(hjust=0, size=12),
        panel.background = element_rect(fill=NA))+
  scale_y_discrete(expand = c(0, 0),position = "right")+
  scale_x_discrete(expand = c(0, 0))+
  labs(x="", y="")
p

## merge the two graphs
### p and dendro.plot have been created by ggplot
library(cowplot)
plot_grid(p, dendro.plot, align = "h")
## Change some details
plot_grid(p, 
          dendro.plot+
            scale_y_discrete(expand=c(0,0))+
            scale_x_discrete(expand=c(0,0))+
            theme(plot.margin = unit(c(0, 0, 0, -0.5), unit="cm")),
          align = "h",
          scale=c(1, 0.95),
          rel_widths=c(0.7, 0.3))

## save plot
pdf("heatmap.pdf", width=5, height=9)
plot_grid(p, 
          dendro.plot+
            scale_y_discrete(expand=c(0,0))+
            scale_x_discrete(expand=c(0,0))+
            theme(plot.margin = unit(c(0, 0, 0, -0.5), unit="cm")),
          align = "h",
          scale=c(1, 0.95),
          rel_widths=c(0.7, 0.3))
dev.off()

参考:
https://jcoliver.github.io/learn-r/009-expression-heatmaps.html
http://www.biotrainee.com/thread-1350-1-1.html
https://cran.r-project.org/web/p ... s/introduction.html



上一篇:贝叶斯网状meta分析
下一篇:问一个大家bam文件修改HI标签的问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|生信技能树    

GMT+8, 2018-10-20 09:07 , Processed in 0.113406 second(s), 26 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.