# 加载R包;
library(pheatmap)
# 查看当前目录的文件;
dir()
## [1] "COX模型.Rmd" "heatmap_pheatmap.html" "heatmap_pheatmap.Rmd"
## [4] "rsconnect" "top30genesexp.csv" "相关性网络图.R"
# 读入 CSV(用第1列做行名)
df <- read.csv("top30genesexp.csv", header = TRUE, row.names = 1, check.names = FALSE)
df <- as.matrix(df); storage.mode(df) <- "numeric"
# 查看数据的前6行,前6列;
df[1:6, 1:6]
## Ss_1 Ss_2 Ss_3 Bc_1 Bc_2 Bc_3
## MSTRG.15233 6715.10 5376.93 7079.48 3205.79 1733.16 1733.17
## Pp3c8_7680 6823.72 4798.55 4794.67 2047.43 962.95 1028.58
## Pp3c20_16230 4107.03 3442.52 9426.14 1738.24 1926.17 1142.52
## Pp3c13_6860 6436.62 4573.53 5095.80 1523.94 3597.34 1872.31
## Pp3c17_9180 6071.54 2842.33 3545.38 2015.36 2436.01 1997.46
## Pp3c4_16930 4386.64 4803.22 8170.27 2121.59 1014.94 2107.13
# 自定义颜色(负值蓝色,正值红色,中间白色)
mycol <- colorRampPalette(c("#4575B4", "white", "#D73027"))(100)
p <- pheatmap(df, scale = "row",
border_color = "white", color = mycol,
show_rownames = FALSE)
p
# 进化聚类树的高度设置(默认50 points);
p2 <- pheatmap(df, scale = "row",
border_color = "white", color = mycol,
show_rownames = FALSE,
legend_breaks = c(-2.5, -1.25, 0, 1.25, 2.5),
legend_labels = c("-2.5","-1.25","0","1.25","2.5"),
treeheight_row = 40,
treeheight_col = 40)
p2
# 调整“横轴”字体方向与大小
p3 <- pheatmap(df, scale = "row",
border_color = "white", color = mycol,
show_rownames = FALSE,
legend_breaks = c(-2.5, -1.25, 0, 1.25, 2.5),
legend_labels = c("-2.5","-1.25","0","1.25","2.5"),
treeheight_row = 40,
treeheight_col = 40,
fontsize = 6,
fontsize_col = 8,
angle_col = 90)
p3
# 生成注释所用数据框(列注释)
ano_col <- data.frame(Cluster = factor(paste0('Cluster', cutree(p2$tree_col, 3))))
order <- p3$tree_col$order
collab <- p3$tree_col$labels
row.names(ano_col) <- collab[order]
p4 <- pheatmap(df, scale = "row",
border_color = "white", color = mycol,
show_rownames = FALSE,
legend_breaks = c(-2.5, -1.25, 0, 1.25, 2.5),
legend_labels = c("-2.5","-1.25","0","1.25","2.5"),
treeheight_row = 40,
treeheight_col = 40,
fontsize = 6,
fontsize_col = 8,
angle_col = 90,
annotation_col = ano_col)
p4
# 类似的也可以生成行标记
ano_row <- data.frame(gene = factor(paste0('Cluster', cutree(p2$tree_row, 9))))
order1 <- p3$tree_row$order
rowlab <- p3$tree_row$labels
row.names(ano_row) <- rowlab[order1]
p5 <- pheatmap(df, scale = "row",
border_color = "white", color = mycol,
show_rownames = FALSE,
legend_breaks = c(-2.5, -1.25, 0, 1.25, 2.5),
legend_labels = c("-2.5","-1.25","0","1.25","2.5"),
treeheight_row = 40,
treeheight_col = 40,
fontsize = 6,
fontsize_col = 8,
angle_col = 90,
annotation_col = ano_col,
annotation_row = ano_row)
p5
p6 <- pheatmap(df, scale = "row",
border_color = "white", color = mycol,
show_rownames = TRUE, # 关键:显示行名(基因名)
show_colnames = TRUE,
labels_row = rownames(df), # 可选:显式指定行标签
fontsize_row = 8, # 行名字号(可按需要调)
fontsize_col = 8,
angle_col = 90,
legend_breaks = c(-2.5, -1.25, 0, 1.25, 2.5),
legend_labels = c("-2.5","-1.25","0","1.25","2.5"),
treeheight_row = 40,
treeheight_col = 40,
annotation_col = ano_col,
annotation_row = ano_row,
cellheight = 10) # 可选:行高,避免名字挤在一起
p6
##小提示: - 如果基因名仍然拥挤:把 fontsize_row
再调小、或把代码块的 fig.height
再调大;也可以适当加大
cellheight
。更多分析关注xhs:飞高高啦