美文网首页R语言学习
R语言可视化(二十四):和弦图绘制

R语言可视化(二十四):和弦图绘制

作者: Davey1220 | 来源:发表于2020-10-13 09:56 被阅读0次

24. 和弦图绘制


清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/24chord/")

使用circlize包绘制和弦图

# 安装并加载所需的R包
# install.packages("circlize")
library(circlize)
## Warning: package 'circlize' was built under R version 3.6.3
## ========================================
## circlize version 0.4.10
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: https://jokergoo.github.io/circlize_book/book/
## 
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization
##   in R. Bioinformatics 2014.
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(circlize))
## ========================================

# 构建示例数据
set.seed(999)
# 构造邻接矩阵
mat = matrix(sample(18, 18), 3, 6)
rownames(mat) = paste0("S", 1:3)
colnames(mat) = paste0("E", 1:6)
head(mat)
##    E1 E2 E3 E4 E5 E6
## S1  4 14 13 17  5  2
## S2  7  1  6  8 12 15
## S3  9 10  3 16 11 18

# 构建邻接列表数据框
df = data.frame(from = rep(rownames(mat), times = ncol(mat)), #起始对象
                to = rep(colnames(mat), each = nrow(mat)), #终止对象
                value = as.vector(mat),#起始对象与终止对象之间的相互作用强度
                stringsAsFactors = FALSE)
head(df)
##   from to value
## 1   S1 E1     4
## 2   S2 E1     7
## 3   S3 E1     9
## 4   S1 E2    14
## 5   S2 E2     1
## 6   S3 E2    10

# 使用chordDiagram函数绘制和弦图
# 使用邻接矩阵绘图
chordDiagram(mat)
# 结束绘图,返回默认设置,否则会继续叠加图层
circos.clear() 
image.png
# 使用邻接列表数据框绘图
chordDiagram(df)
circos.clear()
image.png
# 使用order参数调整外围sectors的排列顺序
chordDiagram(mat, 
             order = c("S2", "S1", "E4", "E1", "S3", 
                       "E5", "E2", "E6", "E3"))
circos.clear()
image.png
# 使用grid.col参数调整外围sectors的填充颜色
grid_col = c(S1 = "red", S2 = "green", S3 = "blue",
             E1 = "yellow", E2 = "pink", E3 = "orange", 
             E4 = "purple", E5 = "black", E6 = "grey")
# transparency参数调整透明度
chordDiagram(mat, 
             grid.col = grid_col,
             transparency = 0.7)
circos.clear()
image.png
# 使用col参数调整links的填充颜色
col_mat = rand_color(length(mat), transparency = 0.5)
head(col_mat)
## [1] "#A4566980" "#91874C80" "#8E9BB680" "#C2D1BC80" "#48484980" "#D1ACBD80"

chordDiagram(mat, 
             col = col_mat)
circos.clear()
image.png
# 使用link.border,link.lty和link.lwd参数设置links的边框颜色,线型和线宽
chordDiagram(mat, 
             link.border = "red",
             link.lty = 2,
             link.lwd = 2)
circos.clear()
image.png
# 使用annotationTrack参数指定外围sectors的类型,可从c("name", "grid", "axis")中指定任意值,也可以指定多个值
chordDiagram(mat, grid.col = grid_col, 
             annotationTrack = "grid" # 指定类型为“gird”只显示网格,不显示刻度线和标签轨道
             ) 
image.png
chordDiagram(mat, grid.col = grid_col, 
             annotationTrack = c("name", "grid"), # 指定显示标签和网格轨道
             annotationTrackHeight = c(0.04, 0.02) # 指定标签和网格轨道的高度
             )  
image.png
chordDiagram(mat, grid.col = grid_col, 
             annotationTrack = NULL) # 去除所有轨道
circos.clear()
image.png
# 使用circos.par函数设置参数
circos.par(clock.wise = FALSE, #逆时针旋转
           start.degree = 60 #起始位置设置为逆时针60度方向
           )
chordDiagram(mat)
circos.clear()
image.png
#设置不同sector之间gap的间隔大小
circos.par(gap.after = c("S1" = 5, "S2" = 8, "S3" = 15, 
                         "E1" = 5, "E2" = 10,"E3" = 5, 
                         "E4" = 3, "E5" = 5, "E6" = 15))
chordDiagram(mat)
circos.clear()
image.png
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Chinese (Simplified)_China.936 
## [2] LC_CTYPE=Chinese (Simplified)_China.936   
## [3] LC_MONETARY=Chinese (Simplified)_China.936
## [4] LC_NUMERIC=C                              
## [5] LC_TIME=Chinese (Simplified)_China.936    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] circlize_0.4.10
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.5          digest_0.6.20       grid_3.6.0         
##  [4] magrittr_1.5        evaluate_0.14       stringi_1.4.3      
##  [7] GlobalOptions_0.1.2 rmarkdown_1.13      tools_3.6.0        
## [10] stringr_1.4.0       xfun_0.8            yaml_2.2.0         
## [13] compiler_3.6.0      colorspace_1.4-1    shape_1.4.4        
## [16] htmltools_0.3.6     knitr_1.23

相关文章

网友评论

    本文标题:R语言可视化(二十四):和弦图绘制

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