RR和R包的学习绘图技巧

R circlize:heatmap, track多图

2020-11-19  本文已影响0人  胡童远

导读

circlize包中circos.heatmap绘制圆版热图,circos.track可追加更多图形成组合图。

修改自:https://jokergoo.github.io/circlize_book/book/circos-heatmap.html
more:https://jokergoo.github.io/circlize_book/book/high-level-plots.html

模拟输入

set.seed(123)
mat1 = rbind(cbind(matrix(rnorm(50*5, mean = 1), nr = 50), 
                   matrix(rnorm(50*5, mean = -1), nr = 50)),
             cbind(matrix(rnorm(50*5, mean = -1), nr = 50), 
                   matrix(rnorm(50*5, mean = 1), nr = 50))
            )
rownames(mat1) = paste0("R", 1:100)
colnames(mat1) = paste0("C", 1:10)
# randomly permute rows 随机排行
mat1 = mat1[sample(100, 100), ]
# 构造分组
split = sample(letters[1:5], 100, replace = TRUE)
split = factor(split, levels = letters[1:5])

一、基础图

library("circlize")
col_fun1 = colorRamp2(c(-2, 0, 2), c("blue", "white", "red"))

pdf("circheatmap_base.pdf")
circos.heatmap(mat1, col = col_fun1)
circos.clear()
dev.off()

二、分组

pdf("circheatmap_split.pdf")
circos.heatmap(mat1, split = split, col = col_fun1)
circos.clear()
dev.off()

三、分组标记

pdf("circheatmap_split_group.pdf")
circos.heatmap(mat1, split = factor(split, levels = c("e", "d", "c", "b", "a")), col = col_fun1, show.sector.labels = TRUE)
circos.clear()
dev.off()

四、加树

pdf("circheatmap_split_group_tree.pdf")
circos.heatmap(mat1, split = split, col = col_fun1, dend.side = "inside")
circos.clear()
dev.off()

五、加外圈标记

pdf("circheatmap_split_group_tree_label.pdf")
circos.heatmap(mat1, split = split, col = col_fun1, dend.side = "inside", rownames.side = "outside")
circos.clear()
dev.off()

六、按分组给树上色

install.packages("dendextend")
library("dendextend")
dend_col = structure(1:5, names = letters[1:5])
pdf("circheatmap_split_group_tree_label_col.pdf")
circos.heatmap(mat1, split = split, col = col_fun1, dend.side = "inside",
    dend.track.height = 0.2,
    dend.callback = function(dend, m, si) {
        color_branches(dend, k = 1, col = dend_col[si])
    }, rownames.side = "outside"
)
circos.clear()
dev.off()

七、两热图组合

mat2 = mat1[sample(100, 100), ] # randomly permute mat1 by rows
col_fun2 = colorRamp2(c(-2, 0, 2), c("green", "white", "red"))
pdf("circheatmap_split_group_tree_label_col_bind.pdf")
circos.heatmap(mat2, col = col_fun2, rownames.side = "outside")
circos.heatmap(mat1, split = split, col = col_fun1, dend.side = "inside",
    dend.track.height = 0.2,
    dend.callback = function(dend, m, si) {
        color_branches(dend, k = 1, col = dend_col[si])
    }
)
circos.clear()
dev.off()

八、热图 + 散点图

row_mean = rowMeans(mat1[, 1:5])
pdf("circheatmap_split_group_tree_label_col_bind_point.pdf")
circos.heatmap.initialize(mat1, split = split)
circos.track(ylim = range(row_mean), panel.fun = function(x, y) {
    y = row_mean[CELL_META$subset]
    y = y[CELL_META$row_order]
    circos.lines(CELL_META$cell.xlim, c(0, 0), lty = 2, col = "grey")
    circos.points(seq_along(y) - 0.5, y, col = ifelse(y > 0, "red", "blue"))
}, cell.padding = c(0.02, 0, 0.02, 0))
circos.heatmap(mat1, col = col_fun1, dend.side = "inside", rownames.side = "outside"
)
circos.clear()
dev.off()

九、热图 + 散点图 + 箱图

pdf("circheatmap_split_group_tree_label_col_bind_point_box.pdf")
circos.heatmap.initialize(mat1, split = split)
circos.track(ylim = range(row_mean), panel.fun = function(x, y) {
    y = row_mean[CELL_META$subset]
    y = y[CELL_META$row_order]
    circos.lines(CELL_META$cell.xlim, c(0, 0), lty = 2, col = "grey")
    circos.points(seq_along(y) - 0.5, y, col = ifelse(y > 0, "red", "blue"))
}, cell.padding = c(0.02, 0, 0.02, 0))
circos.track(ylim = range(mat1), panel.fun = function(x, y) {
    m = mat1[CELL_META$subset, 1:5, drop = FALSE]
    m = m[CELL_META$row_order, , drop = FALSE]
    n = nrow(m)
    circos.boxplot(t(m), pos = 1:n - 0.5, pch = 16, cex = 0.3)
    circos.lines(CELL_META$cell.xlim, c(0, 0), lty = 2, col = "grey")
}, cell.padding = c(0.02, 0, 0.02, 0))
circos.heatmap(mat1, col = col_fun1, dend.side = "inside", rownames.side = "outside")
circos.clear()
dev.off()

十、组合图顺序

pdf("circheatmap_split_group_tree_label_col_bind_point_box_colname.pdf")
circos.par(gap.after = c(2, 2, 2, 2, 10))
circos.heatmap(mat1, split = split, col = col_fun1, track.height = 0.4, rownames.side = "inside")
circos.track(track.index = 1, panel.fun = function(x, y) {
    if(CELL_META$sector.numeric.index == 5) { # the last sector
        cn = colnames(mat1)
        n = length(cn)
        circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), 
            1:n - 0.5, cn, 
            cex = 0.5, adj = c(0, 0.5), facing = "inside")
    }
}, bg.border = NA)
circos.track(ylim = range(mat1), panel.fun = function(x, y) {
    m = mat1[CELL_META$subset, 1:5, drop = FALSE]
    m = m[CELL_META$row_order, , drop = FALSE]
    n = nrow(m)
    circos.boxplot(t(m), pos = 1:n - 0.5, pch = 16, cex = 0.3)
    circos.lines(CELL_META$cell.xlim, c(0, 0), lty = 2, col = "grey")
}, cell.padding = c(0.02, 0, 0.02, 0))
circos.track(ylim = range(row_mean), panel.fun = function(x, y) {
    y = row_mean[CELL_META$subset]
    y = y[CELL_META$row_order]
    circos.lines(CELL_META$cell.xlim, c(0, 0), lty = 2, col = "grey")
    circos.points(seq_along(y) - 0.5, y, col = ifelse(y > 0, "red", "blue"))
}, cell.padding = c(0.02, 0, 0.02, 0))
circos.clear()
dev.off()
上一篇 下一篇

猜你喜欢

热点阅读