专栏名称: 生信技能树
生物信息学学习资料分析,常见数据格式及公共数据库资料分享。常见分析软件及流程,基因检测及癌症相关动态。
目录
相关文章推荐
新京报书评周刊  ·  DeepSeek刷屏之后:人机共创年代,还有 ... ·  3 天前  
上海书评  ·  何贝莉 ... ·  2 天前  
疯子与书  ·  //@栝-cheetahs-每天吃一个西红柿 ... ·  昨天  
悦读文摘  ·  7A抗菌+椰香味保暖背心你见过吗?自带bra ... ·  5 天前  
51好读  ›  专栏  ›  生信技能树

ggalign-瀑布图

生信技能树  · 公众号  ·  · 2024-10-27 09:27

正文

前面我们在 人工智能大模型不会告诉你的热图绘制技巧 演示了如何使用ggplot2热图扩展包(ggalign),可以快速替代之前的 pheatmap:

实际上这个 ggplot2热图扩展包(ggal ign) 的功能不仅于此,还可以绘制肿瘤外显子的常见的图表:瀑布图


安装

你可以使用以下命令从 CRAN 安装 ggalign

install.packages("ggalign")

或者,从 r-universe [1] 安装开发版本(目前开发版本添加了大量新功能,因为CRAN通常需要1-2个月的更新间隔,暂不能推送到CRAN):

install.packages("ggalign",
repos = c("https://yunuuuu.r-universe.dev", "https://cloud.r-project.org")
)

或者从 GitHub [2]

# install.packages("remotes")
remotes::install_github("Yunuuuu/ggalign")

如果您在使用ggalign后觉得它对您的工作有所帮助,欢迎在 https://github.com/Yunuuuu/ggalign 上为此项目添加星星。

输入数据

ggoncoplot 接受一个字符矩阵,用于编码突变类型,您可以使用 ;:,| 来分隔多个突 变,或者任何定义了 fortify_heatmap() 方法的对象。目前能直接接受 maftools 的对象。

mat <- read.table(textConnection(
    "s1,s2,s3
g1,snv; indel,snv,indel
g2,,snv;indel,snv
g3,snv,,indel;snv"

), row.names = 1, header = TRUE, sep = ",", stringsAsFactors = FALSE)

所有操作跟 ggheatmap() 一摸一样,想要更多文档?请参考: https://yunuuuu.github.io/ggalign/dev/

ggoncoplot(mat, map_width = c(snv = 0.5), map_height = c(indel = 0.9)) +
    # Note that guide legends from `geom_tile` and `geom_bar` are different.
    # Although they appear similar, the internal mechanisms won't collapse
    # the guide legends. Therefore, we remove the guide legends from
    # `geom_tile`.
    guides(fill = "none") +
    hmanno("t", size = 0.5) +
    ggalign() +
    geom_bar(aes(fill = value), data = function(x) {
        subset(x, !is.na(value))
    }) +
    hmanno("r", size = 0.5) +
    ggalign() +
    geom_bar(aes(fill = value), orientation = "y", data = function(x) {
        subset(x, !is.na(value))
    }) &
    scale_fill_brewer(palette = "Dark2", na.translate = FALSE)

maftools 对象可直接输入。

laml.maf <- system.file("extdata""tcga_laml.maf.gz", package = "maftools")
laml.clin <- system.file("extdata""tcga_laml_annot.tsv", package = "maftools")
laml <- maftools::read.maf(maf = laml.maf, clinicalData = laml.clin)
ggoncoplot(laml, n_top = 20L, collapse_vars = TRUE) +
    # 去掉x轴 labels
    scale_x_discrete(breaks = NULL) +
    hmanno("r", size = unit(1"cm")) +
    ggalign(
        data = function(x) {
            # by default, `fortify_heatmap` will attach the gene summary
            # into the `gene_anno` attribute
            data <- ggalign_attr(x, "gene_anno")
            cols <- setdiff(
                names(data),
                c(
                    "Tumor_Sample_Barcode""Hugo_Symbol""total",
                    "MutatedSamples""AlteredSamples"
                )
            )
            ans <- as.matrix(data[, ..cols])
            rownames(ans) <- data$Hugo_Symbol
            ans
        }
    ) +
    geom_bar(aes(x = value, .row_names, fill = .column_names),
        stat = "identity"
    ) +
    guides(fill = "none") +
    hmanno("t", size = unit(1"cm")) +
    ggalign(
        data = function(x) {
            # by default, `fortify_heatmap` will attach the sample summary
            # into the `sample_anno` attribute
            data <- ggalign_attr(x, "sample_anno")
            cols <- setdiff(names(data), c("Tumor_Sample_Barcode""total"))
            ans <- as.matrix(data[, ..cols])
            rownames(ans) <- data$Tumor_Sample_Barcode
            ans
        }
    ) +
    geom_bar(aes(y = value, fill = .column_names),
        stat = "identity"
    ) +
    guides(fill = "none") &
    scale_fill_brewer(palette = "Set3", na.translate = FALSE)
参考资料
[1]

r-universe: https://yunuuuu.r-universe.dev/ggalign







请到「今天看啥」查看全文