library(pheatmap)
#导入方式1:读取剪切板中的内容
Peter <- read.table(pipe("pbpaste"))
#导入方式2:读取文件内容
Peter <- read.table("Peter.csv",header=T, sep=",",row.names = 1)
#做热图所需内容格式需要为矩阵,矩阵化
Peter <- as.matrix(Peter)
#获取向量/矩阵/表/数据框/函数的一部分
head(Peter)
#默认绘图
pheatmap(Peter)
#自定义热图颜色+热块边框线有无
pheatmap(Peter, color = colorRampPalette(c("navy", "white", "firebrick3"))(100),
border = FALSE)
#设定边框线色
pheatmap(Peter, border_color = "blue")
#不绘制渐变图的图例
pheatmap(Peter, legend = FALSE)
#热图块内显示数值
pheatmap(Peter, display_numbers = TRUE)
#设定数值字符串的格式及颜色
pheatmap(Peter, display_numbers = TRUE, number_format = "%.1e",number_color = "blue")
#构建矩阵文本,填充热图块
pheatmap(Peter, display_numbers = matrix(ifelse(Peter > 5, "*", ""), nrow(Peter)))
#等同
pheatmap(Peter, display_numbers = ifelse(test > 5, "*", ""))
#添加列注释信息1+将导入的注释信息转化为data.frame格式
annotation_col <- read.csv2("Peter_col.csv",header=T,row.names = 1)
annotation_col <- as.data.frame(annotation_col)
#添加行注释信息1+将导入的注释信息转化为data.frame格式
annotation_row <- read.csv2("Peter_row.csv",header=T,row.names = 1)
annotation_row <- as.data.frame(annotation_row)
#添加列注释信息2
annotation_col = data.frame(
Peter1= c(rep("NO",1),rep("YES",1)),
Peter2 = c(rep("NO",1),rep("YES",1)),
Type = c(rep("Type1",1),rep("Type2",1)
)
row.names(annotation_col) <- colnames(Peter)
#添加行注释信息2
annotation_row = data.frame(Pathway = c(rep("Pathway1",1),rep("Pathway2",1))
row.names(annotation_row) <- rownames(Peter)
#查看行注释内容
View(annotation_row)
#查看行注释内容
View(annotation_col)
#定义注释内容颜色
ann_colors = list(
Peter1 = c(NO = "#FFFFFF", YES = "#000000"),
Peter2 = c(NO = "#FFFFFF", YES = "#000000"),
Type = c(Type1 = "#FFAEB9", Type2 = "#FF7F00")
)
#pheatmap的各种参数
pheatmap(
Peter,
show_colnames=F,
cluster_cols = FALSE, cluster_rows = FALSE,
cellwidth = 6.5, cellheight = 6.5, fontsize = 4.5,
gaps_row = c(1,3,5),
gaps_col = c(1,3,5),
row_split = annotation_row$Pathway,#按行分类截断
column_split = annotation_col$Peter1,#按列分类截断
annotation_col = annotation_col, annotation_row = annotation_row,
annotation_colors = ann_colors, annotation_legend = FALSE
)
R语言学习笔记:Pheatmap包
于 2023-12-08 22:59:05 首次发布