circlize
包
circlize
包在德國癌癥中心的華人博士Zuguang Gu開發(fā)的测暗,有興趣的可以去看看他的Github主頁蒜魄。這個包有兩個文檔彪蓬,一個是介紹基本原理的繪制簡單圈圈圖的捂齐,也是本次要介紹的。另外一份文檔專門介紹基因組數(shù)據(jù)繪制圈圈圖Genomic Circos Plot,我自己還沒看完蚊惯,下次再介紹侠鳄。
根據(jù)我的學習發(fā)現(xiàn)這個包與ggplot2
很相似异剥,也是先創(chuàng)建一個圖層愚屁,然后不斷的添加圖形元素(point
济竹、line
、bar
等)霎槐,這些簡單的圖形元素都有circos.
這個前綴進行繪制送浊,比如要繪制點,則用circos.points()
丘跌。具體的下面一一介紹袭景。
用circlize
繪制圈圈圖
照例,沒有安裝這個包的先安裝:
install.packages("circlize")
或者devtools::install_github("jokergoo/circlize")
闭树。
library(circlize)
# 簡單創(chuàng)建一個數(shù)據(jù)集
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
繪制圖是不斷疊加的,因此如果我們一大段代碼下來我們只能看到最終的圖形仰猖,這里為了演示每端代碼的結(jié)果捏肢,所以每次我都得初始化以及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數(shù)目鸵赫,初始化里只有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,具體數(shù)目由factors決定的膨疏,向本數(shù)據(jù)集中factors有八個一睁,因此繪制一個track,其包含八個cell佃却。含有前綴circos.track的函數(shù)會在所有的cel里添加基本元素者吁,而只有前綴circos.的函數(shù)可以在特定的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
會自動創(chuàng)建一個track
育八,無需我們circos.trackPlotRegion
進行創(chuàng)建对途。
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()
創(chuàng)建第三個track
這里又得提一下,當我們繪制多個
track
時髓棋,我們添加基本元素時要指定添加到哪個track
(track.index
指定)实檀、哪個cell
(sector.index
指定)里,如果不指定仲锄,那么將默認track
是我們剛剛創(chuàng)建的那個劲妙。track.index
、sector.index
等參數(shù)可以通過get.cell.meta.data
函數(shù)獲取儒喊。
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镣奋,因為我們剛剛創(chuàng)建,這里這一步不用也可怀愧。
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()
實際操作中我們常常會更新數(shù)據(jù)或者想更新圖形侨颈,這是可以通過
circos.updatePlotRegion
函數(shù)在特定的track
、cell
里(先刪除再添加)update
芯义,下面我們將通過circos.updatePlotRegion
函數(shù)先刪除track 2
哈垢、sector 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")
接下來添加
links
扛拨,links
可以是point
到point
耘分、point
到interval
、interval
到interval
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
的繪圖規(guī)則是初始化(initialize
)-創(chuàng)建track
-添加圖形元素-創(chuàng)建track
-添加圖形元素-…-circos.clear
绑警。具體參數(shù)設(shè)置以及解釋由于內(nèi)容太多求泰,有興趣的可以自己參考文檔。 我認為比較重要的是要理解track
计盒、sector
渴频。由于基本所有的圖形元素我們都是添加在sector
里面,因此就需要指定track.index
以及sector.index
北启。接下來就用個例子來講解一下如何操縱track
卜朗、sector
。
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肩钠,不然下次無法繪圖泣港。
放大某一特定區(qū)域
df <- data.frame(factors = sample(letters[1:6], 100, replace = TRUE),
x = rnorm(100),
y = rnorm(100),
stringsAsFactors = FALSE)
# 放大a暂殖,b區(qū)域
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設(shè)置
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包的原理,繪制基因組數(shù)據(jù)的圖形也是一樣的当纱。有時間下次介紹(主要是我自己還沒看完呛每,看不太懂)
。
老規(guī)矩坡氯,給出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