ixxmu / mp_duty

抓取网络文章到github issues保存
https://archives.duty-machine.now.sh/
119 stars 30 forks source link

ggalign-瀑布图 #5803

Closed ixxmu closed 5 hours ago

ixxmu commented 5 hours ago

https://mp.weixin.qq.com/s/y5M36Ve84zUptEsJeSPv5A

ixxmu commented 5 hours ago

ggalign-瀑布图 by 生信协作组

安装

你可以使用以下命令从 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

[2]

GitHub: https://github.com/Yunuuuu/ggalign