因为一直没有认真学习过R,都是用到啥去查找啥的,所以每次画图总觉得力不从心。最近加了个群,准备跟着群主大咖认真系统的学习一些常见图形。
今天就学习一下我们最常用的普通热图以及环形热图的画法。
这个是我们今天用的测试数据:
测试数据一普通热图还是用常用的pheatmap包:
library(pheatmap)
data <- as.matrix(read.table("data.txt",row.names = 1,header = T,sep = "\t"))
pheatmap(data) //下面就是默认的色系画出的图
其它重要的参数设置:
1. scale="row" //设置是按行或者列标准化
比如下面就是设置了按行进行标准化
pheatmap(data,scale="row")
2. cluster_rows=F,cluster_cols=F //这2个参数设置要不要对行或者列进行聚类
fontsize_row=15,fontsize_col=20,angle_col=315 //这几个参数则是设置行名,列名的大小和角度
pheatmap(data,scale="row",cluster_rows=F,cluster_cols=F,fontsize_row=15,fontsize_col=20,angle_col=315)
3. annotation_row,annotation_col //添加注释文件
annotation_col <- data.frame(Sample=factor(c("A","B","C","D","E","F","G","H","I","J")))
rownames(annotation_col)<-colnames(data)
annotation_row <- data.frame(CellType=rep(c("A","B"),9))
rownames(annotation_row)<-rownames(data)
annotation_col <- data.frame(Sample=factor(c("A","B","C","D","E","F","G","H","I","J")),Sex=rep(c("F","M"),5))
rownames(annotation_col)<-colnames(data)
pheatmap(data,annotation_col=annotation_col)
3. ann_colors //自定义annoation的颜色
annotation_col <- data.frame(Sample=factor(c("A","B","C","D","E","F","G","H","I","J")),Sex=rep(c("F","M"),5))
rownames(annotation_col)<-colnames(data)
annotation_row <- data.frame(CellType=rep(c("T1","T2"),9))
rownames(annotation_row)<-rownames(data)
ann_colors = list(Sex = c(F = "red", M = "#016D06"),CellType= c(T1 = "#1B9E77", T2 = "#D95F02"),Sample = c(A = "#65B99F", B = "#F08961", C = "#8A9BC3", D = "#DA85B5", E = "#A1CC56",F = "#F5D239", G = "#7CC07B", H = "#BAABD0", I = "#3766A4", J = "#DF3078"))
pheatmap(data,annotation_col=annotation_col,annotation_row=annotation_row,annotation_colors=ann_colors)
4. 当然还有一些其他重要的参数:
color //可以自己设置heatmap的色系
cellwidth=30,cellheight=14 //设置单元格的宽度和高度
border_color=F //设置显示不显示边界线
display_numbers=T //设置显示heatmap上面的数字
例如下图是对参数进行一系列调整的结果:
pheatmap(data,annotation_col=annotation_col,annotation_row=annotation_row,annotation_colors=ann_colors,fontsize_col=8,fontsize_row=10,angle_col=45,cellwidth=30,cellheight=14,display_numbers=T,border_color=F,color=colorRampPalette(c("#FDEBEA","#D5281F"))(100))
环形热图我见paper中也有用的,但是总觉得没普通的好用,并且如果变量太多的时候还不够直观。主要是基于ggtree和gheatmap实现的。
library(ggtree)
library(ggplot2)
data <- as.matrix(read.table("data.txt",row.names = 1,header = T,sep = "\t"))
df <- hclust(dist(data))
p1 <- ggtree(df)
gheatmap(p1,data)
p2 <- ggtree(df,layout = "circular")
p3 <- rotate_tree(p2,100) //设置旋转角度
gheatmap(p3 + geom_tiplab(offset = 13),data,
width = 1.5, //热图的宽度
low = "#FDEBEA",
high = "#D5281F", //low和high主要设置热图的色系
font.size = 3,
colnames_position = "top",
colnames_offset_y = 2.5, //调整开口的大小
hjust = 0 //调整列名和顶部的距离
) +
theme(legend.position = "right")
网友评论