From d3a9e4f691f245063bacec496ddfe32009cb57d8 Mon Sep 17 00:00:00 2001 From: freefire <97975089@qq.com> Date: Sat, 11 Jan 2025 21:47:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.R | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 main.R diff --git a/main.R b/main.R new file mode 100644 index 0000000..6dbca89 --- /dev/null +++ b/main.R @@ -0,0 +1,100 @@ +# 列出所需的包 +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() \ No newline at end of file