05.热图

作者: BassBassU | 来源:发表于2022-03-14 14:51 被阅读0次
a1 <- rnorm(100)
dim(a1) <- c(5,20)

pheatmap::pheatmap(a1)
Rplot01.png
a2 <- rnorm(100)+2
dim(a2)=c(5,20)
pheatmap::pheatmap(a2)
Rplot01.png
pheatmap(cbind(a1,a2))
将两个表放在一起画热图;但是在这个图里,对a1,a2里面的每一列自动排序了 Rplot02.png
pheatmap(cbind(a1,a2),cluster_cols=F)
#cluster_cols=F, 不让数据内部排序,整体a1,a2比较
不让数据内部排序,整体a1,a2比较 Rplot04.png

给变量名字

b <- cbind(a1,a2)
b <- as.data.frame(b)
把b变成数据框,每一列就有了名字 数据框有名字
给数据框的行列命名
a1 <- rnorm(100)
dim(a1) <- c(5,20)
a2 <- rnorm(100)+2
dim(a2)=c(5,20)
b <- cbind(a1,a2)
b <- as.data.frame(b)
names(b)=c(paste('a1',1:20,sep = '_'),paste('a2',1:20,sep = '_'))
pheatmap::pheatmap(b)
Rplot01.png
pheatmap::pheatmap(b,cluster_cols=F)
cluster_cols=F取消排序 cluster_cols=F取消排序
代码拆解、给代码命名
b <- cbind(a1,a2)
b <- as.data.frame(b)
names(b)=c(paste('a1',1:20,sep = '_'),paste('a2',1:20,sep = '_'))
pheatmap::pheatmap(b)
> paste('a1',1:20)
 [1] "a1 1"  "a1 2"  "a1 3"  "a1 4"  "a1 5"  "a1 6"  "a1 7"  "a1 8" 
 [9] "a1 9"  "a1 10" "a1 11" "a1 12" "a1 13" "a1 14" "a1 15" "a1 16"
[17] "a1 17" "a1 18" "a1 19" "a1 20"

> paste('a1',1:20,sep = '_')#可能会出现'a1 1 _',把每行代码分别运行就会好
 [1] "a1_1"  "a1_2"  "a1_3"  "a1_4"  "a1_5"  "a1_6"  "a1_7"  "a1_8" 
 [9] "a1_9"  "a1_10" "a1_11" "a1_12" "a1_13" "a1_14" "a1_15" "a1_16"
[17] "a1_17" "a1_18" "a1_19" "a1_20"

c(paste('a1',1:20,sep = '_'),paste('a2',1:20,sep = '_'))
 [1] "a1_1"  "a1_2"  "a1_3"  "a1_4"  "a1_5"  "a1_6"  "a1_7"  "a1_8" 
 [9] "a1_9"  "a1_10" "a1_11" "a1_12" "a1_13" "a1_14" "a1_15" "a1_16"
[17] "a1_17" "a1_18" "a1_19" "a1_20" "a2_1"  "a2_2"  "a2_3"  "a2_4" 
[25] "a2_5"  "a2_6"  "a2_7"  "a2_8"  "a2_9"  "a2_10" "a2_11" "a2_12"
[33] "a2_13" "a2_14" "a2_15" "a2_16" "a2_17" "a2_18" "a2_19" "a2_20"

names(b) <- c(paste('a1',1:20,sep = '_'),paste('a2',1:20,sep = '_'))
pheatmap::pheatmap(b)
 #paste(c('a1','a2'),1:40,sep = '_')#自己随便改的
 [1] "a1_1"  "a2_2"  "a1_3"  "a2_4"  "a1_5"  "a2_6"  "a1_7"  "a2_8" 
 [9] "a1_9"  "a2_10" "a1_11" "a2_12" "a1_13" "a2_14" "a1_15" "a2_16"
[17] "a1_17" "a2_18" "a1_19" "a2_20" "a1_21" "a2_22" "a1_23" "a2_24"
[25] "a1_25" "a2_26" "a1_27" "a2_28" "a1_29" "a2_30" "a1_31" "a2_32"
[33] "a1_33" "a2_34" "a1_35" "a2_36" "a1_37" "a2_38" "a1_39" "a2_40"
学习包的方法
?pheatmap

把实例中的example中的代码打进去,运行

# Create test matrix
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")

# Draw heatmaps
pheatmap(test)
pheatmap(test, kmeans_k = 2)
pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
pheatmap(test, cluster_row = FALSE)
pheatmap(test, legend = FALSE)
创建矩阵
test = matrix(rnorm(200), 20, 10)
屏幕截图 2022-03-15 181445.png
给矩阵做出数据差
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
#这些代码的意义就是为该矩阵创建一个差,使其能分析出差异
#第一行:取1:10行,取(1,3,5,7,9)列,每一个数值+3

#seq(1, 10, 2)
[1] 1 3 5 7 9
test[1:10, seq(1, 10, 2)] 
#取1:10行,取(1,3,5,7,9)列

          [,1]     [,2]     [,3]     [,4]     [,5]
 [1,] 2.461216 3.941112 3.540708 3.560276 4.494728
 [2,] 3.017270 4.145309 4.372118 1.917767 2.806879
 [3,] 3.290116 2.102957 2.491475 2.399375 2.986099
 [4,] 3.715698 3.567388 4.810664 3.169702 4.545596
 [5,] 3.400343 2.615763 3.375856 2.532111 3.040975
 [6,] 3.721175 2.348180 3.922074 2.988972 3.835639
 [7,] 3.800795 3.864160 4.222244 4.485441 1.907291
 [8,] 2.242058 3.922189 2.307712 2.665809 4.960910
 [9,] 3.205740 2.752304 3.891801 3.791830 1.377026
[10,] 3.069984 2.516898 2.709539 3.738636 4.990948
已有数值梯度但是行名列名未变化 屏幕截图 2022-03-15 182719.png
给test行列命名
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
行名列名已修改 屏幕截图 2022-03-15 182758.png
绘制test热图
pheatmap::pheatmap(test)
Rplot02.png
修改test热图
pheatmap::pheatmap(test, kmeans_k = 2)

cluster是把一堆基因当做一个值,低频需求


Rplot03.png
pheatmap::pheatmap(test, scale = "row", clustering_distance_rows = "correlation")

侧面从10至-2变位为2至-2,放大了差异,让颜色加深


Rplot04.png
pheatmap::pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))

改颜色


Rplot05.png
pheatmap::pheatmap(test, cluster_row = FALSE)
Rplot06.png
pheatmap::pheatmap(test, legend = FALSE)
Rplot07.png
pheatmap::pheatmap(test, display_numbers = TRUE)

每一个空格加上数字


Rplot04.png
pheatmap::pheatmap(test, display_numbers = TRUE, number_format = "\%.1e")

报错

pheatmap::pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))

大于固定值的加"*"


Rplot02.png
pheatmap::pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",
"1e-4", "1e-3", "1e-2", "1e-1", "1"))
Rplot03.png
pheatmap::pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")

调整格式


Rplot05.png
pheatmap::pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")

将其保存为pdf

添加行、列
# Generate annotations for rows and columns
annotation_col = data.frame(
                    CellType = factor(rep(c("CT1", "CT2"), 5)), 
                    Time = 1:5
                )
rownames(annotation_col) = paste("Test", 1:10, sep = "")

annotation_row = data.frame(
                    GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
                )
rownames(annotation_row) = paste("Gene", 1:20, sep = "")

准备行名、列名


屏幕截图 2022-03-15 202348.png
屏幕截图 2022-03-15 202413.png
data.frame(
+     CellType = factor(rep(c("CT1", "CT2"), 5)), 
+     Time = 1:5
+ )
   CellType Time
1       CT1    1
2       CT2    2
3       CT1    3
4       CT2    4
5       CT1    5
6       CT2    1
7       CT1    2
8       CT2    3
9       CT1    4
10      CT2    5
> rownames(annotation_col) = paste("Test", 1:10, sep = "")
> View(annotation_col)
pheatmap::pheatmap(test, annotation_col = annotation_col, annotation_legend = FALSE)
Rplot.png
pheatmap::pheatmap(test, annotation_col = annotation_col, annotation_legend = TRUE)
Rplot01.png
pheatmap::pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)
Rplot06.png

相关文章

  • R语言可视化(五):密度分布图绘制

    05.密度分布图绘制 清除当前环境中的变量 设置工作目录 基础plot函数绘制密度分布图 ggplot2包绘制密度...

  • complexheatmap学习3——热图拼接

    最基本的例子 标题 热图的大小 热图之间的空隙 基于主要的热图自动调整 主要热图的设置 注释部分的调整 热图和注释...

  • 热图

    R绘图基础(四)热图 heatmap:https://qiubio.com/archives/2477 那些年画过...

  • 热图

    「热图」ComplexHeatmap展示单细胞聚类:http://xuzhougeng.top/archives/...

  • 热图

    Create test matrix Draw heatmaps Show text within cells F...

  • 【热图】

    2021.4.28持续更新中。。。 参考:《R数据可视化手册》、学术数据分析及可视化[https://space....

  • 热图

    有点小瑕疵 参考:木舟笔记

  • 热图

    静态与交互式热图: heatmap():用于绘制简单热图的函数heatmap.2():绘制增强热图的函数d3hea...

  • 热图

    成品图: 核心要点: 左边和上面的分组以及最主要的核心数据 所需要的数据: 原始数据(属水平的相对丰度表) 目的:...

  • 1 初识Complexheatmap

    ComplexHeatmap绘制的热图种类分为: ① Heatmap 类:单个热图,包括热图主体、行/列名称、标题...

网友评论

      本文标题:05.热图

      本文链接:https://www.haomeiwen.com/subject/uychdrtx.html