a1 <- rnorm(100)
dim(a1) <- c(5,20)
pheatmap::pheatmap(a1)
a2 <- rnorm(100)+2
dim(a2)=c(5,20)
pheatmap::pheatmap(a2)
pheatmap(cbind(a1,a2))
将两个表放在一起画热图;但是在这个图里,对a1,a2里面的每一列自动排序了
pheatmap(cbind(a1,a2),cluster_cols=F)
#cluster_cols=F, 不让数据内部排序,整体a1,a2比较
不让数据内部排序,整体a1,a2比较
给变量名字
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)
pheatmap::pheatmap(b,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)
给矩阵做出数据差
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
已有数值梯度但是行名列名未变化
给test行列命名
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
行名列名已修改
绘制test热图
pheatmap::pheatmap(test)
修改test热图
pheatmap::pheatmap(test, kmeans_k = 2)
cluster是把一堆基因当做一个值,低频需求
pheatmap::pheatmap(test, scale = "row", clustering_distance_rows = "correlation")
侧面从10至-2变位为2至-2,放大了差异,让颜色加深
pheatmap::pheatmap(test, color = colorRampPalette(c("navy", "white", "firebrick3"))(50))
改颜色
pheatmap::pheatmap(test, cluster_row = FALSE)
pheatmap::pheatmap(test, legend = FALSE)
pheatmap::pheatmap(test, display_numbers = TRUE)
每一个空格加上数字
pheatmap::pheatmap(test, display_numbers = TRUE, number_format = "\%.1e")
报错
pheatmap::pheatmap(test, display_numbers = matrix(ifelse(test > 5, "*", ""), nrow(test)))
大于固定值的加"*"
pheatmap::pheatmap(test, cluster_row = FALSE, legend_breaks = -1:4, legend_labels = c("0",
"1e-4", "1e-3", "1e-2", "1e-1", "1"))
pheatmap::pheatmap(test, cellwidth = 15, cellheight = 12, main = "Example heatmap")
调整格式
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 = "")
准备行名、列名
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)
pheatmap::pheatmap(test, annotation_col = annotation_col, annotation_legend = TRUE)
pheatmap::pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row)