R绘图-热图

差异基因热图绘制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#
# Create heat map from a differential expression count table.
#
# Load the library.
suppressPackageStartupMessages(library(gplots))

# The name of the file that contains the counts.
count_file = "results.csv"

# The name of the output file.
output_file = "heatmap.pdf"

# Inform the user.
print("# Tool: Create Heatmap ")
print(paste("# Input: ", count_file))
print(paste("# Output: ", output_file))

# FDR cutoff.
MIN_FDR = 0.05

# Plot width
WIDTH = 12

# Plot height.
HEIGHT = 13

# Set the margins
MARGINS = c(9, 12)

# Relative heights of the rows in the plot.
LHEI = c(1, 5)

# Read normalized counts from the standard input.
data = read.csv(count_file, header=T, as.is=TRUE)

# Subset data for values under a treshold.
data = subset(data, data$FDR <= MIN_FDR)

# The heatmap row names will be taken from the first column.
row_names = data[, 1]

# The code assumes that the normalized data matrix is listed to the right of the falsePos column.
idx = which(colnames(data) == "falsePos") + 1

# The normalized counts are on the right size.
counts = data[, idx : ncol(data)]

# Load the data from the second column on.
values = as.matrix(counts)

# Adds a little noise to each element to avoid the
# clustering function failure on zero variance rows.
values = jitter(values, factor = 1, amount = 0.00001)

# Normalize each row to a z-score
zscores = NULL
for (i in 1 : nrow(values)) {
row = values[i,]
zrow = (row - mean(row)) / sd(row)
zscores = rbind(zscores, zrow)
}

# Set the row names on the zscores.
row.names(zscores) = row_names

# Turn the data into a matrix for heatmap2.
zscores = as.matrix(zscores)

# Set the color palette.
col = greenred

# Create a PDF device
pdf(output_file, width = WIDTH, height = HEIGHT)

heatmap.2(zscores, col=col, density.info="none", Colv=NULL,
dendrogram="row", trace="none", margins=MARGINS, lhei=LHEI)

dev.off()

heatmap.png

普通热图

热图效果

横轴为样本类型,纵向为基因ID

数据准备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Gene	Sample1	Sample2	Sample3	Sample4	Sample5	Sample6	Sample7	Sample8	Sample9	Sample10
Gene1 0.60 1.06 0.00 0.25 0.78 2.05 0.42 0.57 0.41 0.57
Gene2 4.03 4.05 0.94 0.00 4.49 4.10 1.94 0.69 0.23 0.98
Gene3 0.06 0.19 0.36 0.61 0.64 0.39 0.15 0.20 0.52 0.20
Gene4 2.55 1.86 0.16 0.42 1.16 4.39 0.42 0.00 0.35 0.00
Gene5 0.89 0.91 0.00 0.00 0.10 1.25 0.35 0.28 0.48 0.28
Gene6 2.75 5.02 0.04 0.00 1.41 5.76 2.08 0.00 0.98 0.00
Gene7 0.66 0.90 0.00 0.53 0.34 0.59 0.75 0.39 0.40 0.18
Gene8 0.90 2.87 0.36 0.17 0.06 0.06 0.97 0.08 0.00 0.26
Gene9 7.15 5.97 0.00 0.00 1.70 8.46 3.49 9.88 0.12 0.27
Gene10 3.12 3.41 0.52 0.15 0.36 3.44 0.90 2.09 0.15 2.08
Gene11 0.24 0.64 0.00 0.00 0.15 0.88 0.31 0.00 0.57 0.08
Gene12 4.31 5.32 0.00 0.17 1.88 3.94 1.30 0.97 0.51 1.32
Gene13 2.27 2.09 0.08 0.00 0.00 1.78 1.45 1.17 1.20 1.37
Gene14 0.87 0.27 0.00 0.00 0.00 0.64 0.00 0.00 0.00 0.00
Gene15 3.37 3.63 1.26 0.29 2.41 4.08 1.78 0.06 0.91 0.06
Gene16 0.18 1.09 0.15 0.37 1.28 0.72 0.23 0.30 1.56 0.30
Gene17 3.56 3.82 0.18 0.00 1.10 5.36 1.48 4.76 1.23 5.31
Gene18 1.77 1.54 1.62 0.00 0.20 4.82 0.91 0.25 0.36 0.25

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 加载包
rm(list = ls())
library(pheatmap)


# 导入数据
data <- as.matrix(read.table("data.txt",row.names = 1,header = T,sep = "\t"))


# 构建列注释信息
annotation_col = data.frame(CellType = factor(c("A","B","C","D","E",
"F","G","H","I","J")))

rownames(annotation_col) <- colnames(data)
head(annotation_col)


# 自定注释信息的颜色列表
ann_colors = list(
CellType = c(A = "#65B99F", B = "#F08961", C = "#8A9BC3", D = "#DA85B5", E = "#A1CC56",
F = "#F5D239", G = "#7CC07B", H = "#BAABD0", I = "#3766A4", J = "#DF3078"))

head(ann_colors)

pheatmap(data,
# 去掉聚类树:
cluster_cols = FALSE,
cluster_rows = FALSE,
# 加color bar:列注释信息;
annotation_col = annotation_col,
# color bar 颜色设定:
annotation_colors = ann_colors,
# 设置单元格颜色渐变;(100)表示分100段渐变;
color = colorRampPalette(c("#FDEBEA","#D5281F"))(100),
# 行、列标签的字体大小
fontsize_col = 8,
fontsize_row = 10,
# 是否显示行、列名
show_colnames = F,
# 设置每个单元格的宽度和高度
cellwidth = 30,
cellheight = 24,
# 行、列聚类树的高度:
# treeheight_row = 50,
# treeheight_col = 30,
# display_numbers = TRUE参数设定在每个热图格子中显示相应的数值,
# number_color参数设置数值字体的颜色
display_numbers = TRUE,number_color = "black",
# 设置标题:
main = "Heatmap")

circle 热图

热图效果

image-20230510124031974

数据准备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Gene	Sample1	Sample2	Sample3	Sample4	Sample5	Sample6	Sample7	Sample8	Sample9	Sample10
Gene1 0.60 1.06 0.00 0.25 0.78 2.05 0.42 0.57 0.41 0.57
Gene2 4.03 4.05 0.94 0.00 4.49 4.10 1.94 0.69 0.23 0.98
Gene3 0.06 0.19 0.36 0.61 0.64 0.39 0.15 0.20 0.52 0.20
Gene4 2.55 1.86 0.16 0.42 1.16 4.39 0.42 0.00 0.35 0.00
Gene5 0.89 0.91 0.00 0.00 0.10 1.25 0.35 0.28 0.48 0.28
Gene6 2.75 5.02 0.04 0.00 1.41 5.76 2.08 0.00 0.98 0.00
Gene7 0.66 0.90 0.00 0.53 0.34 0.59 0.75 0.39 0.40 0.18
Gene8 0.90 2.87 0.36 0.17 0.06 0.06 0.97 0.08 0.00 0.26
Gene9 7.15 5.97 0.00 0.00 1.70 8.46 3.49 9.88 0.12 0.27
Gene10 3.12 3.41 0.52 0.15 0.36 3.44 0.90 2.09 0.15 2.08
Gene11 0.24 0.64 0.00 0.00 0.15 0.88 0.31 0.00 0.57 0.08
Gene12 4.31 5.32 0.00 0.17 1.88 3.94 1.30 0.97 0.51 1.32
Gene13 2.27 2.09 0.08 0.00 0.00 1.78 1.45 1.17 1.20 1.37
Gene14 0.87 0.27 0.00 0.00 0.00 0.64 0.00 0.00 0.00 0.00
Gene15 3.37 3.63 1.26 0.29 2.41 4.08 1.78 0.06 0.91 0.06
Gene16 0.18 1.09 0.15 0.37 1.28 0.72 0.23 0.30 1.56 0.30
Gene17 3.56 3.82 0.18 0.00 1.10 5.36 1.48 4.76 1.23 5.31
Gene18 1.77 1.54 1.62 0.00 0.20 4.82 0.91 0.25 0.36 0.25

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 加载包
rm(list = ls())
library(ggtree)

# 导入数据
data <- as.matrix(read.table("data.txt",row.names = 1,header = T,sep = "\t"))

# 进行层次聚类
df <- hclust(dist(data))

# 绘制进化树
p1 <- ggtree(df)
p1

# 查看是否合适
gheatmap(p1,data)

# 将进化树绘制成圆形
p2 <- ggtree(df,layout = "circular")
p2

# 设置开口方向:rotate_tree()
# 顺时针旋转100度
p3 <- rotate_tree(p2,100)
P3


gheatmap(p3 + geom_tiplab(offset = 13),data,
# 设置热图的宽度:
width = 1.5,
# 设置单元格的颜色:
low = "#FDEBEA",
high = "#D5281F",
font.size = 3,
colnames_position = "top",
# 调整开口大小
colnames_offset_y = 1,
# 调节列名和顶部之间的距离:
hjust = 0
) +
theme(legend.position = "right")



# 文字和图的位置后续可以在Ai中调整;