用circlize包繪制circos plot

circlize

circlize包在德國癌癥中心的華人博士Zuguang Gu開發的,有興趣的可以去看看他的Github主頁。這個包有兩個文檔,一個是介紹基本原理的繪制簡單圈圈圖的,也是本次要介紹的。另外一份文檔專門介紹基因組數據繪制圈圈圖Genomic Circos Plot,我自己還沒看完,下次再介紹。
根據我的學習發現這個包與ggplot2很相似,也是先創建一個圖層,然后不斷的添加圖形元素(pointlinebar等),這些簡單的圖形元素都有circos.這個前綴進行繪制,比如要繪制點,則用circos.points()。具體的下面一一介紹。

circlize繪制圈圈圖

照例,沒有安裝這個包的先安裝:install.packages("circlize")或者devtools::install_github("jokergoo/circlize")

library(circlize)
# 簡單創建一個數據集
set.seed(999)
n <- 1000
a <- data.frame(factors = sample(letters[1:8], n, replace = TRUE), x = rnorm(n), y = runif(n))

繪圖第一步是先初始化(circos.initialize),接下來繪制track,再添加基本元素。需要提一下的是,由于circlize繪制圖是不斷疊加的,因此如果我們一大段代碼下來我們只能看到最終的圖形,這里為了演示每端代碼的結果,所以每次我都得初始化以及circlize.clear

繪制第一個track

par(mar = c(1, 1, 1, 1), lwd = 0.1, cex = 0.6)
circos.par(track.height = 0.1)
circos.initialize(factors = a$factors, x = a$x) #初始化,factors來控制track數目,初始化里只有x, 沒有y。這一步相當于ggplot()
circos.trackPlotRegion(factors = a$factors, y = a$y, 
panel.fun = function(x, y) { 
circos.axis()})
col <- rep(c("#FF0000", "#00FF00"), 4) #自定義一下顏色# 這里先解釋一下,一個track有好幾個cell,具體數目由factors決定的,向本數據集中factors有八個,因此繪制一個track,其包含八個cell。含有前綴circos.track的函數會在所有的cel里添加基本元素,而只有前綴circos.的函數可以在特定的track、cell里添加基本元素。具體看下演示。
circos.trackPoints(a$factors, a$x, a$y, col = col, pch = 16, cex = 0.5) #所有的cell里都繪制點圖
circos.text(-1, 0.5, "left", sector.index = "a", track.index = 1) #在track 1中的標記為a的cell里添加
textcircos.text(1, 0.5, "right", sector.index = "a")
circos.clear()

接下來繪制第二個track

circos.trackHist添加柱狀圖,由于柱狀圖相對高級一點,因此circos.trackHist會自動創建一個track,無需我們circos.trackPlotRegion進行創建。

par(mar = c(1, 1, 1, 1), lwd = 0.1, cex = 0.6)
circos.par(track.height = 0.1)
circos.initialize(factors = a$factors, x = a$x)
circos.trackPlotRegion(factors = a$factors, y = a$y, 
panel.fun = function(x, y) { 
circos.axis()})
col <- rep(c("#FF0000", "#00FF00"), 4)
circos.trackPoints(a$factors, a$x, a$y, col = col, pch = 16, cex = 0.5)
circos.text(-1, 0.5, "left", sector.index = "a", track.index = 1)
circos.text(1, 0.5, "right", sector.index = "a")
bg.col <- rep(c("#EFEFEF", "#CCCCCC"), 4)
circos.trackHist(a$factors, a$x, bg.col = bg.col, col = NA)
circos.clear()

創建第三個track

這里又得提一下,當我們繪制多個track時,我們添加基本元素時要指定添加到哪個track(track.index指定)、哪個cell(sector.index指定)里,如果不指定,那么將默認track是我們剛剛創建的那個。track.indexsector.index等參數可以通過get.cell.meta.data函數獲取。

par(mar = c(1, 1, 1, 1), lwd = 0.1, cex = 0.6)
circos.par(track.height = 0.1)
circos.initialize(factors = a$factors, x = a$x)
circos.trackPlotRegion(factors = a$factors, y = a$y,
 panel.fun = function(x, y) { 
circos.axis()})
col <- rep(c("#FF0000", "#00FF00"), 4)
circos.trackPoints(a$factors, a$x, a$y, col = col, pch = 16, cex = 0.5)
circos.text(-1, 0.5, "left", sector.index = "a", track.index = 1)
circos.text(1, 0.5, "right", sector.index = "a")
bg.col <- rep(c("#EFEFEF", "#CCCCCC"), 4)
circos.trackHist(a$factors, a$x, bg.col = bg.col, col = NA)
circos.trackPlotRegion(factors = a$factors, x = a$x, y = a$y, 
panel.fun = function(x, y) {
 grey = c("#FFFFFF", "#CCCCCC", "#999999") 
sector.index = get.cell.meta.data("sector.index") #這個是第三個track,因為我們剛剛創建,這里這一步不用也可。
 xlim = get.cell.meta.data("xlim")
 ylim = get.cell.meta.data("ylim") 
circos.text(mean(xlim), mean(ylim), sector.index) 
circos.points(x[1:10], y[1:10], col = "red", pch = 16, cex = 0.6) 
circos.points(x[11:20], y[11:20], col = "blue", cex = 0.6)})
circos.clear()

實際操作中我們常常會更新數據或者想更新圖形,這是可以通過circos.updatePlotRegion函數在特定的trackcell里(先刪除再添加)update,下面我們將通過circos.updatePlotRegion函數先刪除track 2sector d中的圖形元素再添加點圖。

par(mar = c(1, 1, 1, 1), lwd = 0.1, cex = 0.6)
circos.par(track.height = 0.1)
circos.initialize(factors = a$factors, x = a$x)
circos.trackPlotRegion(factors = a$factors, y = a$y, 
panel.fun = function(x, y) { 
circos.axis()})col <- rep(c("#FF0000", "#00FF00"), 4)
circos.trackPoints(a$factors, a$x, a$y, col = col, pch = 16, cex = 0.5)
circos.text(-1, 0.5, "left", sector.index = "a", track.index = 1)
circos.text(1, 0.5, "right", sector.index = "a")
circos.trackPoints(a$factors, a$x, a$y, col = col, pch = 16, cex = 0.5)
circos.text(-1, 0.5, "left", sector.index = "a", track.index = 1)
circos.text(1, 0.5, "right", sector.index = "a")
bg.col <- rep(c("#EFEFEF", "#CCCCCC"), 4)
circos.trackHist(a$factors, a$x, bg.col = bg.col, col = NA)
circos.trackPlotRegion(factors = a$factors, x = a$x, y = a$y,
 panel.fun = function(x, y) { 
grey = c("#FFFFFF", "#CCCCCC", "#999999") 
sector.index = get.cell.meta.data("sector.index")
 xlim = get.cell.meta.data("xlim") 
ylim = get.cell.meta.data("ylim") 
circos.text(mean(xlim), mean(ylim), sector.index)
circos.points(x[1:10], y[1:10], col = "red", pch = 16, cex = 0.6)
 circos.points(x[11:20], y[11:20], col = "blue", cex = 0.6)})
# update第2個track中標記為d的sector
circos.updatePlotRegion(sector.index = "d", track.index = 2)
circos.points(x = -2:2, y = rep(0, 5))
xlim <- get.cell.meta.data("xlim")
ylim <- get.cell.meta.data("ylim")
circos.text(mean(xlim), mean(ylim), "updated")
circos.clear()

接下來繪制第四個track

par(mar = c(1, 1, 1, 1), lwd = 0.1, cex = 0.6)
circos.par(track.height = 0.1)
circos.initialize(factors = a$factors, x = a$x)
circos.trackPlotRegion(factors = a$factors, y = a$y,
 panel.fun = function(x, y) { circos.axis()})
col <- rep(c("#FF0000", "#00FF00"), 4)
circos.trackPoints(a$factors, a$x, a$y, col = col, pch = 16, cex = 0.5)
circos.text(-1, 0.5, "left", sector.index = "a", track.index = 1)
circos.text(1, 0.5, "right", sector.index = "a")
circos.trackPoints(a$factors, a$x, a$y, col = col, pch = 16, cex = 0.5)
circos.text(-1, 0.5, "left", sector.index = "a", track.index = 1)
circos.text(1, 0.5, "right", sector.index = "a")
bg.col <- rep(c("#EFEFEF", "#CCCCCC"), 4)
circos.trackHist(a$factors, a$x, bg.col = bg.col, col = NA)
circos.trackPlotRegion(factors = a$factors, x = a$x, y = a$y, 
panel.fun = function(x, y) { 
grey = c("#FFFFFF", "#CCCCCC", "#999999") 
sector.index = get.cell.meta.data("sector.index") 
xlim = get.cell.meta.data("xlim")
 ylim = get.cell.meta.data("ylim") 
circos.text(mean(xlim), mean(ylim), sector.index) 
circos.points(x[1:10], y[1:10], col = "red", pch = 16, cex = 0.6) 
circos.points(x[11:20], y[11:20], col = "blue", cex = 0.6)})
# update第2個track中標記為d的sector
circos.updatePlotRegion(sector.index = "d", track.index = 2)
circos.points(x = -2:2, y = rep(0, 5))
xlim <- get.cell.meta.data("xlim")
ylim <- get.cell.meta.data("ylim")
circos.text(mean(xlim), mean(ylim), "updated")
circos.clear()
circos.trackPlotRegion(factors = a$factors, y = a$y)
circos.trackLines(a$factors[1:100], a$x[1:100], a$y[1:100], type = "h")

接下來添加linkslinks可以是pointpointpointintervalintervalinterval

par(mar = c(1, 1, 1, 1), lwd = 0.1, cex = 0.6)
circos.par(track.height = 0.1)
circos.initialize(factors = a$factors, x = a$x)
circos.trackPlotRegion(factors = a$factors, y = a$y,
 panel.fun = function(x, y) { circos.axis()})
col <- rep(c("#FF0000", "#00FF00"), 4)
circos.trackPoints(a$factors, a$x, a$y, col = col, pch = 16, cex = 0.5)
circos.text(-1, 0.5, "left", sector.index = "a", track.index = 1)
circos.text(1, 0.5, "right", sector.index = "a")
circos.trackPoints(a$factors, a$x, a$y, col = col, pch = 16, cex = 0.5)
circos.text(-1, 0.5, "left", sector.index = "a", track.index = 1)
circos.text(1, 0.5, "right", sector.index = "a")
bg.col <- rep(c("#EFEFEF", "#CCCCCC"), 4)
circos.trackHist(a$factors, a$x, bg.col = bg.col, col = NA)
circos.trackPlotRegion(factors = a$factors, x = a$x, y = a$y,
 panel.fun = function(x, y) {
 grey = c("#FFFFFF", "#CCCCCC", "#999999") 
sector.index = get.cell.meta.data("sector.index") 
xlim = get.cell.meta.data("xlim") 
ylim = get.cell.meta.data("ylim")
circos.text(mean(xlim), mean(ylim), sector.index) 
circos.points(x[1:10], y[1:10], col = "red", pch = 16, cex = 0.6) 
circos.points(x[11:20], y[11:20], col = "blue", cex = 0.6)})
# update第2個track中標記為d的sector
circos.updatePlotRegion(sector.index = "d", track.index = 2)
circos.points(x = -2:2, y = rep(0, 5))
xlim <- get.cell.meta.data("xlim")
ylim <- get.cell.meta.data("ylim")
circos.text(mean(xlim), mean(ylim), "updated")
circos.clear()
circos.trackPlotRegion(factors = a$factors, y = a$y)
circos.trackLines(a$factors[1:100], a$x[1:100], a$y[1:100], type = "h")
circos.link("a", 0, "b", 0, h = 0.3) #point to point
circos.link("c", c(-0.5, 0.5), "d", c(-0.5, 0.5), col = "red", border = NA, h = 0.2) #intreval to interval
circos.link("e", 0, "g", c(-1, 1), col = "green", border = "black", lwd = 2, lty = 2) #point to interval

circlize詳述

circlize的繪圖規則是初始化(initialize)-創建track-添加圖形元素-創建track-添加圖形元素-…-circos.clear。具體參數設置以及解釋由于內容太多,有興趣的可以自己參考文檔。 我認為比較重要的是要理解tracksector。由于基本所有的圖形元素我們都是添加在sector
里面,因此就需要指定track.index以及sector.index。接下來就用個例子來講解一下如何操縱tracksector

par(mar = c(1, 1, 1, 1))
factors <- letters[1:8]
circos.initialize(factors = factors, xlim = c(0, 1)) #初始化# 繪制三個track,并顯示具體信息
for (i in 1:3) { 
circos.trackPlotRegion(ylim = c(0, 1))}
circos.info(plot = TRUE)
# 通過draw.sector()來高亮某一sector,比如a:
draw.sector(get.cell.meta.data("cell.start.degree", sector.index = "a"), 
get.cell.meta.data("cell.end.degree", sector.index = "a"), rou1 = 1, col = "blue")
circos.clear()
# 高亮某一track, 比如第一個track:
circos.initialize(factors = factors, xlim = c(0, 1))
for (i in 1:3) { 
circos.trackPlotRegion(ylim = c(0, 1))}
circos.info(plot = TRUE)
draw.sector(0, 360, rou1 = get.cell.meta.data("cell.top.radius", track.index = 1), 
rou2 = get.cell.meta.data("cell.bottom.radius", track.index = 1), col = "green")
circos.clear()
# 高亮某一track某一sector,比如地2、3track中的e、f(sector):
circos.initialize(factors = factors, xlim = c(0, 1))
for (i in 1:3) { circos.trackPlotRegion(ylim = c(0, 1))}
circos.info(plot = TRUE)
draw.sector(get.cell.meta.data("cell.start.degree", sector.index = "e"), 
get.cell.meta.data("cell.end.degree", sector.index = "f"), 
get.cell.meta.data("cell.top.radius", track.index = 2), 
get.cell.meta.data("cell.bottom.radius", track.index = 3), col = "red")
circos.clear() #千萬別忘了circos.clear,不然下次無法繪圖。

放大某一特定區域

df <- data.frame(factors = sample(letters[1:6], 100, replace = TRUE),
                          x = rnorm(100), 
                          y = rnorm(100), 
                          stringsAsFactors = FALSE)
# 放大a,b區域
zoom_df <- df %>% dplyr::filter(factors %in% c("a", "b"))
zoom_df$factors <- paste0("zoom_", zoom_df$factors)
df2 <- rbind(df, zoom_df)
xrange <- tapply(df2$x, df2$factors, function(x) max(x) - min(x))
normal_sector_index <- unique(df$factors)
zoomed_sector_index <- unique(zoom_df$factors)
sector.width <- c(xrange[normal_sector_index]/sum(xrange[normal_sector_index]), 
xrange[zoomed_sector_index]/sum(xrange[zoomed_sector_index]))
# 繪圖
par(mar = c(1, 1, 1, 1))
circos.par(start.degree = 90)
circos.initialize(df2$factors, x = df2$x, sector.width = sector.width)
circos.trackPlotRegion(df2$factors, x = df2$x, y = df2$y, 
panel.fun = function(x, y) { 
circos.points(x, y, col = "red", pch = 16, cex = 0.5) 
xlim = get.cell.meta.data("xlim") 
ylim = get.cell.meta.data("ylim") 
sector.index = get.cell.meta.data("sector.index") 
circos.text(mean(xlim), mean(ylim), sector.index, niceFacing = TRUE)})
# 添加links
circos.link("a", get.cell.meta.data("cell.xlim", sector.index = "a"), "zoom_a", 
get.cell.meta.data("cell.xlim", sector.index = "zoom_a"), border = NA, col = "red")

circos.clear()

舉個栗子

圈圈圖+熱圖+進化樹

set.seed(1234)
data <- matrix(rnorm(100 * 10), nrow = 10, ncol = 100)
col <- colorRamp2(c(-2, 0, 2), c("green", "black", "red"))
factors <- rep(letters[1:2], times = c(30, 70))
data_list <- list(a = data[, factors == "a"], b = data[, factors == "b"])
dend_list <- list(a = as.dendrogram(hclust(dist(t(data_list[["a"]])))), 
                  b = as.dendrogram(hclust(dist(t(data_list[["b"]])))))
circos.par(cell.padding = c(0, 0, 0, 0), gap.degree = 5)
circos.initialize(factors = factors, xlim = cbind(c(0, 0), table(factors)))
circos.track(ylim = c(0, 10), bg.border = NA, 
panel.fun = function(x, y) {
 sector.index = get.cell.meta.data("sector.index") 
d = data_list[[sector.index]] 
dend = dend_list[[sector.index]] 
d2 = d[, order.dendrogram(dend)] 
col_data = col(d2)
nr = nrow(d2)
nc = ncol(d2) 
for (i in 1:nr) { 
circos.rect(1:nc - 1, rep(nr - i, nc), 1:nc, rep(nr - i + 1, nc),
border = col_data[i, ], col = col_data[i, ]) }})
max_height <- max(sapply(dend_list, function(x) attr(x, "height")))
circos.track(ylim = c(0, max_height), 
bg.border = NA, track.height = 0.3,
panel.fun = function(x, y) { 
sector.index = get.cell.meta.data("sector.index")
dend = dend_list[[sector.index]]
circos.dendrogram(dend, max_height = max_height)})
circos.clear()

多圖排列

直接用layout設置

layout(matrix(1:9, 3, 3))
for (i in 1:9) {
 factors = letters[1:8] 
par(mar = c(0.5, 0.5, 0.5, 0.5)) 
circos.par(cell.padding = c(0, 0, 0, 0)) 
circos.initialize(factors = factors, xlim = c(0, 1)) 
circos.trackPlotRegion(ylim = c(0, 1), track.height = 0.05, 
bg.col = rand_color(8), bg.border = NA) 
# 繪制links 
for (i in 1:20) {
se = sample(letters[1:8], 2) 
circos.link(se[1], runif(2), se[2], runif(2),
col = rand_color(1, transparency = 0.4), border = NA) 
} 
circos.clear()

sessionInfo

理解circlize包的原理,繪制基因組數據的圖形也是一樣的。有時間下次介紹(主要是我自己還沒看完,看不太懂)


老規矩,給出sessionInfo

sessionInfo()
## R version 3.4.0 (2017-04-21)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 8.1 x64 (build 9600)
## ## 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.0 BiocInstaller_1.26.0 forcats_0.2.0
## [4] stringr_1.2.0 dplyr_0.5.0 purrr_0.2.2.2 
## [7] readr_1.1.1 tidyr_0.6.3 tibble_1.3.1
## [10] ggplot2_2.2.1 tidyverse_1.1.1.9000
#### loaded via a namespace (and not attached):
## [1] shape_1.4.2 clisymbols_1.2.0 reshape2_1.4.2 
## [4] haven_1.0.0 lattice_0.20-35 colorspace_1.3-2
## [7] htmltools_0.3.6 yaml_2.1.14 rlang_0.1.1 
## [10] foreign_0.8-68 DBI_0.6-1 modelr_0.1.0
## [13] readxl_1.0.0 plyr_1.8.4 munsell_0.4.3 
## [16] gtable_0.2.0 cellranger_1.1.0 rvest_0.3.2 
## [19] GlobalOptions_0.0.12 psych_1.7.5 evaluate_0.10 
## [22] knitr_1.16 parallel_3.4.0 broom_0.4.2 
## [25] Rcpp_0.12.11 scales_0.4.1 backports_1.1.0 
## [28] formatR_1.5 jsonlite_1.4 boxes_0.0.0.9000
## [31] mnormt_1.5-5 hms_0.3 digest_0.6.12 
## [34] stringi_1.1.5 grid_3.4.0 rprojroot_1.2
## [37] tools_3.4.0 magrittr_1.5 lazyeval_0.2.0 
## [40] crayon_1.3.2.9000 xml2_1.1.1 lubridate_1.6.0 
## [43] assertthat_0.2.0 rmarkdown_1.5 httr_1.2.1 
## [46] rstudioapi_0.6 R6_2.2.1 nlme_3.1-131
## [49] compiler_3.4.0
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容