美文网首页生信相关生信分析~做图
弦图,管他中不中用,反正高大上

弦图,管他中不中用,反正高大上

作者: 小洁忘了怎么分身 | 来源:发表于2020-09-07 17:34 被阅读0次

    0.准备输入数据

    可以是矩阵

    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
    

    行是一边,列是一边,矩阵正文的数字表示连接的强度。

    可以是tidy数据框

    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
    

    一行是一个连接关系,第一列和第二列是要连接的两边,第三列是连接的强度。

    2.简单画图

    chordDiagram(mat)
    
    circos.clear()
    chordDiagram(df)
    
    circos.clear()
    

    circos.clear()是清除原来的弦图设置。你会发现颜色不一样,这是因为每次画图,颜色是随机的

    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) 
    

    从这两张图可以看到,弦默认与行的颜色一致。

    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) # 产生连续色块并指定透明度
    chordDiagram(mat, grid.col = border_color, col = linkcolor2)
    
    circos.clear()
    

    相关文章

      网友评论

        本文标题:弦图,管他中不中用,反正高大上

        本文链接:https://www.haomeiwen.com/subject/fplqektx.html