查看原文
其他

跟着Nature学绘图——热图绘制并添加注释!

王志山 科研后花园 2023-09-08
 

点击上方

“科研后花园”

关注我们

复现图片如下:

主要复现的图片是上图中的图a,具体代码如下:

1、设置工作环境并加载所需R包:

rm(list=ls())#clear Global Environmentsetwd('D:/热图')#设置工作路径
#加载R包library (pheatmap)

2、加载绘图数据(根据原文图片随机编写数据,无实际意义):

df <- read.table(file="data.txt",sep="\t",row.names = 1, header=T,check.names=FALSE)head(df)[1:5,]

3、Z-score转换以保留数据的真实差异

df1 <-df[apply(df,1,var)!=0,] ##去掉方差为0的行,也就是值全都一致的行df_zscore <-as.data.frame(t(apply(df1,1,scale)))#标准化数据,获得Z-scorecolnames(df_zscore)<- colnames(df)

4、初步绘图

pheatmap(df_zscore, angle_col = "45", cellwidth=25, cellheight=8, treeheight_col = 15)

5、设置颜色

pheatmap(df_zscore, angle_col = "45", cellwidth=25, cellheight=8, treeheight_col = 15, color=colorRampPalette(c("#3952a2","black","#f5ea14"))(100))

6、添加行注释信息

annotation_col<- data.frame( "Treatment" = c("Saline","Saline","Saline","Cocaine","Saline", "LSD","LSD","LSD","LSD","Saline", "Saline","Saline","MDMA","MDMA","MDMA", "Ketamine","Ketamine","Ketamine","Ketamine","Ketamine"), "Batch" = c("1","1","1","1","2", "1","2","3","2","1", "2","2","2","1","2", "1","3","3","3","3"), "Post_treatment" = c("48 h","2 wk","2 wk","48 h","48 h", "48 h","2 wk","48 h","48 h","2 wk", "48 h","2 wk","2 wk","2 wk","2 wk", "2 wk","48 h","48 h","48 h","48 h"), "Critical_period" = c("Closed","Closed","Closed","Closed","Closed", "Closed","Closed","Closed","Closed","Open", "Open","Open","Open","Open","Open", "Open","Open","Open","Open","Open"))#行注释矩阵rownames(annotation_col) = colnames(df_zscore)colors <- list("Treatment" = c(Saline = "#000000", Cocaine = "#575757",LSD = "#e79600",MDMA="#a42422",Ketamine="#c53a8e"), "Batch" = c( "1"= "#3953a3", "2" = "#ef4a4a", "3" = "#009848"), "Post_treatment" = c("48 h"="#64838c","2 wk"="#2c3a3e"), "Critical_period" = c(Closed="#94c83d",Open="#4e2469"))pheatmap(df_zscore, angle_col = "45", cellwidth=25, cellheight=8, treeheight_col = 15, color=colorRampPalette(c("#3952a2","black","#f5ea14"))(100), annotation_col = annotation_col, annotation_colors = colors, show_colnames = F)

7、美化(通过调节细节参数以及结合AI进行美化):

pheatmap(df_zscore, angle_col = "45", cellwidth=25, cellheight=8, treeheight_col = 15, color=colorRampPalette(c("#3952a2","black","#f5ea14"))(100), annotation_col = annotation_col, annotation_colors = colors, show_colnames = F, fontsize_row=9, fontsize=12, labels_row = as.expression(lapply(rownames(df_zscore),function(x) bquote(italic(.(x))))),#行名斜体 filename = "heatmap.pdf")

PS: 以上内容是小编个人学习代码笔记分享,仅供参考学习,欢迎大家一起交流学习。
代码及源数据获取:https://github.com/wzsBio/code

推荐阅读


1

扩增子测序数据分析还不会?小编整理的全套R语言代码助您轻松解决问题!

2

R语言速成,这两本书值得您购买(五折优惠)!

3

R语言绘图及数据分析合集!

4

跟着Nature学绘图——分组直方图!

温馨提示

如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。





您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存