0.準(zhǔn)備輸入數(shù)據(jù)
可以是矩陣
library(circlize)
set.seed(999)
mat = sample(0:5,21,replace = T)
mat = matrix(mat,nrow = 3)
rownames(mat) = paste0("S",1:nrow(mat))
colnames(mat) = paste0("G",1:ncol(mat))
mat
## G1 G2 G3 G4 G5 G6 G7
## S1 2 0 1 2 5 1 2
## S2 3 5 1 4 2 0 1
## S3 4 0 5 5 2 4 3
行是一邊鸡典,列是一邊源请,矩陣正文的數(shù)字表示連接的強(qiáng)度枪芒。
可以是tidy數(shù)據(jù)框
library(tidyr)
library(tibble)
df <- gather(rownames_to_column(as.data.frame(mat),var = "from"),
key = "to",
value = "value",
- from)
head(df)
## from to value
## 1 S1 G1 2
## 2 S2 G1 3
## 3 S3 G1 4
## 4 S1 G2 0
## 5 S2 G2 5
## 6 S3 G2 0
一行是一個(gè)連接關(guān)系,第一列和第二列是要連接的兩邊谁尸,第三列是連接的強(qiáng)度舅踪。
2.簡(jiǎn)單畫圖
chordDiagram(mat)
circos.clear()
chordDiagram(df)
circos.clear()
circos.clear()是清除原來的弦圖設(shè)置。你會(huì)發(fā)現(xiàn)顏色不一樣良蛮,這是因?yàn)槊看萎媹D抽碌,顏色是隨機(jī)的
3.自定義邊的顏色
這些顏色出自RColorBrewer包,是我喜歡的配色决瞳,可以自己修改
library(RColorBrewer)
col = c(brewer.pal(n = 12,name = "Set3"),
brewer.pal(n = 8,name = "Set2"))
col
## [1] "#8DD3C7" "#FFFFB3" "#BEBADA" "#FB8072" "#80B1D3" "#FDB462" "#B3DE69"
## [8] "#FCCDE5" "#D9D9D9" "#BC80BD" "#CCEBC5" "#FFED6F" "#66C2A5" "#FC8D62"
## [15] "#8DA0CB" "#E78AC3" "#A6D854" "#FFD92F" "#E5C494" "#B3B3B3"
border_color <- sample(col,nrow(mat)+ncol(mat))
chordDiagram(mat, grid.col = border_color, transparency = 0.7)
chordDiagram(t(mat), grid.col = border_color)
從這兩張圖可以看到货徙,弦默認(rèn)與行的顏色一致。
4.自定義弦的顏色
還是從剛才的顏色里面取
linkcolor <- sample(col,length(mat),replace = T)
dim(linkcolor) <- dim(mat)
chordDiagram(mat, grid.col = border_color, col = linkcolor,transparency = 0.7)
circos.clear()
linkcolor2 <- colorRamp2(range(mat), c("#FA0A0A", "#FFFFFF"),
transparency = 0.5) # 產(chǎn)生連續(xù)色塊并指定透明度
chordDiagram(mat, grid.col = border_color, col = linkcolor2)
circos.clear()