This repository has been archived on 2025-01-13. You can view files and clone it, but cannot push or open issues or pull requests.
2025-01-11 21:47:31 +08:00

100 lines
2.8 KiB
R

# 列出所需的包
packages <- c("ComplexHeatmap", "circlize", "RColorBrewer", "dendextend", "dendsort", "gridBase")
# 检查并安装缺失的包
install_if_missing <- function(pkg) {
if (!requireNamespace(pkg, quietly = TRUE)) {
if (pkg == "ComplexHeatmap") {
if (!requireNamespace("BiocManager", quietly = TRUE)) {
install.packages("BiocManager")
}
BiocManager::install("ComplexHeatmap")
} else {
install.packages(pkg, dependencies = TRUE)
}
}
}
# 应用函数到每个包
sapply(packages, install_if_missing)
# 加载所有包
lapply(packages, library, character.only = TRUE)
# 加载绘图数据
data <- read.table(file = 'E:/hui.zhang/Drawpic/1.csv', header = TRUE, row.names = 1, sep = ',')
# 查看数据
head(data)
# 转化matrix格式矩阵及数据归一化
data <- as.matrix(data)
cir1 <- t(scale(t(data)))
# 处理 NA 值
cir1[is.na(cir1)] <- 0
# 查看归一化后的数据
head(cir1)
# 定义热图颜色梯度
mycol <- colorRamp2(c(-2.5, 0.3, 3.1), c("blue", "white", "red"))
mycol1 <- colorRamp2(c(-2, 0, 2), c("#003399", "white", "#cccc00"))
# 默认参数绘制普通热图
Heatmap(cir1, row_names_gp = gpar(fontsize = 6),
column_names_gp = gpar(fontsize = 8),
col = mycol,
name = "legend")
# 计算数据大小范围
range(cir1)
# 绘制基础环形热图
circos.heatmap(cir1, col = mycol)
circos.clear()
# 在circos.heatmap()中添加参数进行环形热图的调整和美化
circos.par(gap.after = c(30))
circos.heatmap(cir1, col = mycol,
dend.side = "inside",
rownames.side = "outside",
rownames.col = "black",
rownames.cex = 1.2,
rownames.font = 1.2,
bg.border = "black",
cluster = TRUE)
circos.clear()
# 聚类树的调整和美化
circos.par(gap.after = c(30))
circos.heatmap(cir1, col = mycol, dend.side = "inside",
rownames.side = "outside",
track.height = 0.28,
rownames.col = "black",
rownames.cex = 1.3,
rownames.font = 1.3,
cluster = TRUE,
dend.track.height = 0.18,
dend.callback = function(dend, m, si) {
color_branches(dend, k = 15, col = 1:15)
})
circos.clear()
# 添加图例标签等
lg <- Legend(title = "Exp", col_fun = mycol,
direction = c("vertical"))
grid.draw(lg)
# 添加列名
circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {
if (CELL_META$sector.numeric.index == 1) {
cn <- colnames(cir1)
n <- length(cn)
circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(0.5, "mm"),
1:n + 5,
cn, cex = 0.6, adj = c(0, 0.5), facing = "inside")
}
}, bg.border = NA)
circos.clear()