```
##代碼1(標(biāo)準(zhǔn)化圈圖)
##安裝包
install.packages("dendextend","circlize","openxlsx")
##加載包
library(circlize)
library(dendextend)
library(openxlsx)
##讀入數(shù)據(jù)
mat1 <- read.xlsx("C:\\Users\\user\\Desktop\\工作空間\\熱圖\\data.xlsx")##讀入xlsx表
#((C:\\Users\\xyf\\Desktop\user\工作空間\\熱圖\\data.xlsx)替換為你的數(shù)據(jù)的路徑)
row.names(mat1)<-mat1[,1]#修改行名
mat1<-mat1[,-1]##刪除第一列,使之變?yōu)閿?shù)字矩陣,繪圖的數(shù)據(jù)要求為矩陣(也就是單一類型的數(shù)據(jù)矩陣,這里全為數(shù)字)
##繪圖
col_fun1 = colorRamp2(c(-2, 0, 2), c("blue", "white", "red"))##設(shè)置熱圖顏色
column_od = hclust(dist(t(mat1)))$order #對(duì)列聚類
circos.par(gap.after = c(10))##為添加列名留出空間
circos.heatmap(mat1[, column_od], ##將列聚類后重新排序的矩陣
col = col_fun1, ##設(shè)置顏色
dend.side = "inside",##樹狀圖在圈內(nèi)
rownames.side = "outside",##行名在圈外
dend.track.height = 0.2,
dend.callback = function(dend, m, si) {
# when k = 1, it renders one same color for the whole dendrogram
color_branches(dend, k = 4, col = 2:5)##對(duì)樹狀圖進(jìn)行著色
}
)
circos.track(track.index = 2, ##將列名添加在第二個(gè)軌道(就是熱圖所在的環(huán)形軌道)
panel.fun = function(x, y) {
if(CELL_META$sector.numeric.index == 1) { # the last sector
cn = colnames(mat1[, column_od])##取得列名
n = length(cn)
circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), ##x軸坐標(biāo)
1:n - convert_y(0.5, "mm"), ##y軸坐標(biāo)
cn, ##輸入要展示的列名
cex = 0.25, ##列名的大小
adj = c(0, 0.5),
facing = "inside")
}
}, bg.border = NA)
circos.clear()
```
細(xì)致講解
第一步:準(zhǔn)備輸入數(shù)據(jù)。
一般用熱圖展示基因在樣本中的表達(dá)變化,代碼中data.xlsx為輸入文件,格式圖1。
圖1
第二步:安裝軟件(R和Rstudio)和包("dendextend","circlize","openxlsx")。
從這兩個(gè)網(wǎng)址https://cran.r-project.org/bin/windows/base/R-4.1.2-win.exe和https://download1.rstudio.org/desktop/windows/RStudio-2021.09.1-372.exe(復(fù)制到瀏覽器即可打開)下載軟件,直接雙擊安裝,所有選項(xiàng)默認(rèn)。
用下面的代碼2安裝三個(gè)包。
```
##代碼2
##安裝分析包
install.packages("dendextend","circlize","openxlsx")
##加載包
library(circlize)
library(dendextend)
library(openxlsx)
```
第三步:代碼參數(shù)調(diào)整。
1> 修改顏色,使用代碼3,結(jié)果見圖2。顏色編號(hào)("#26B9CB", "#FFFFFF", "#B72865")可以在該網(wǎng)址:https://htmlcolorcodes.com/ 隨意選擇。
```
##代碼3
col_fun1 = colorRamp2(c(-2, 0, 2), c("#26B9CB", "#FFFFFF", "#B72865"))##設(shè)置熱圖顏色
column_od = hclust(dist(t(mat1)))$order #對(duì)列聚類
circos.par(gap.after = c(10))##為添加列名留出空間
circos.heatmap(mat1[, column_od], ##將列聚類后重新排序的矩陣
col = col_fun1, ##設(shè)置顏色
dend.side = "inside",##樹狀圖在圈內(nèi)
rownames.side = "outside",##行名在圈外
dend.track.height = 0.2,
dend.callback = function(dend, m, si) {
# when k = 1, it renders one same color for the whole dendrogram
color_branches(dend, k = 4, col = 2:5)##對(duì)樹狀圖進(jìn)行著色
}
)
circos.track(track.index = 2, ##將列名添加在第二個(gè)軌道(就是熱圖所在的環(huán)形軌道)
panel.fun = function(x, y) {
if(CELL_META$sector.numeric.index == 1) { # the last sector
cn = colnames(mat1[, column_od])##取得列名
n = length(cn)
circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), ##x軸坐標(biāo)
1:n - convert_y(0.5, "mm"), ##y軸坐標(biāo)
cn, ##輸入要展示的列名
cex = 0.25, ##列名的大小
adj = c(0, 0.5),
facing = "inside")
}
}, bg.border = NA)
circos.clear()
```
圖2
2> 修改文字的大小(rownames.cex)。字體(rownames.font)和顏色(rownames.col),使用代碼4,結(jié)果見圖3。
```
##代碼4
col_fun1 = colorRamp2(c(-2, 0, 2), c("#26B9CB", "#FFFFFF", "#B72865"))##設(shè)置熱圖顏色
column_od = hclust(dist(t(mat1)))$order #對(duì)列聚類
circos.par(gap.after = c(15))##為添加列名留出空間
circos.heatmap(mat1[, column_od], ##將列聚類后重新排序的矩陣
col = col_fun1, ##設(shè)置顏色
dend.side = "inside",##樹狀圖在圈內(nèi)
rownames.side = "outside",##行名在圈外
rownames.col = 1:nrow(mat1) %% 10 + 1,##行名添加顏色
rownames.cex = runif(nrow(mat1), min = 0.3, max = 2),##行名的大小
rownames.font = 1:nrow(mat1) %% 4 + 1,##行名字體
dend.track.height = 0.2,
dend.callback = function(dend, m, si) {
# when k = 1, it renders one same color for the whole dendrogram
color_branches(dend, k = 4, col = 2:5)##對(duì)樹狀圖進(jìn)行著色
}
)
circos.track(track.index = 2, ##將列名添加在第二個(gè)軌道(就是熱圖所在的環(huán)形軌道)
panel.fun = function(x, y) {
if(CELL_META$sector.numeric.index == 1) { # the last sector
cn = colnames(mat1[, column_od])##取得列名
n = length(cn)
circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), ##x軸坐標(biāo)
1:n - convert_y(0.5, "mm"), ##y軸坐標(biāo)
cn, ##輸入要展示的列名
cex = 0.25, ##列名的大小
adj = c(0, 0.5),
facing = "inside")
}
}, bg.border = NA)
circos.clear()
```