当前位置: 首页 > article >正文

R包:ggalign调整和组合多个图形的R包

在这里插入图片描述

文章目录

    • 介绍
    • 案例
    • 安装R包
    • 教程1
    • 教程2
    • 参考

介绍

这个包扩展了ggplot2,提供了用于对齐和组织多个图的高级工具,特别是那些自动重新排序观察结果的工具,比如树形图。它提供了对布局调整和情节注释的精细控制,使您能够创建复杂的、出版质量的可视化,同时仍然使用熟悉的ggplot2语法。

This package extends ggplot2 by providing advanced tools for aligning and organizing multiple plots, particularly those that automatically reorder observations, such as dendrogram. It offers fine control over layout adjustment and plot annotations, enabling you to create complex, publication-quality visualizations while still using the familiar grammar of ggplot2.

案例

在这里插入图片描述

安装R包

install.packages("ggalign")

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

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

教程1

library(ggalign)

expr <- read_example("gene_expression.rds")
mat <- as.matrix(expr[, grep("cell", colnames(expr))])
base_mean <- rowMeans(mat)
mat_scaled <- t(apply(mat, 1, scale))
type <- gsub("s\\d+_", "", colnames(mat))

heat1 <- ggheatmap(mat_scaled) -
    scheme_align(free_spaces = "l") +
    scale_y_continuous(breaks = NULL) +
    scale_fill_viridis_c(option = "magma") +
    # add dendrogram for this heatmap
    anno_top() +
    align_dendro() +
    # add a block for the heatmap column
    ggalign(data = type, size = unit(1, "cm")) +
    geom_tile(aes(y = 1, fill = factor(value))) +
    scale_y_continuous(breaks = NULL, name = NULL) +
    scale_fill_brewer(
        palette = "Set1", name = "type",
        guide = guide_legend(position = "top")
    )

heat2 <- ggheatmap(base_mean, width = unit(2, "cm")) +
    scale_y_continuous(breaks = NULL) +
    scale_x_continuous(name = "base mean", breaks = FALSE) +
    scale_fill_gradientn(colours = c("#2600D1FF", "white", "#EE3F3FFF")) +
    # set the active context of the heatmap to the top
    # and set the size of the top stack
    anno_top(size = unit(4, "cm")) +
    # add box plot in the heatmap top
    ggalign() +
    geom_boxplot(aes(y = value, fill = factor(.extra_panel))) +
    scale_x_continuous(expand = expansion(), breaks = NULL) +
    scale_fill_brewer(
        palette = "Dark2", name = "base mean",
        guide = guide_legend(position = "top")
    ) +
    theme(axis.title.y = element_blank())

heat3 <- ggheatmap(expr$type, width = unit(2, "cm")) +
    scale_fill_brewer(palette = "Set3", name = "gene type") +
    scale_x_continuous(breaks = NULL, name = "gene type") +
    # add barplot in the top annotation, and remove the spaces in the y-axis
    anno_top() -
    scheme_align(free_spaces = "lr") +
    ggalign() +
    geom_bar(
        aes(.extra_panel, fill = factor(value)),
        position = position_fill()
    ) +
    scale_y_continuous(expand = expansion()) +
    scale_fill_brewer(palette = "Set3", name = "gene type", guide = "none") -
    scheme_theme(plot.margin = margin())

stack_alignh(mat_scaled) +
    stack_active(sizes = c(0.2, 1, 1)) +
    # group stack rows into 5 groups
    align_kmeans(centers = 5L) +
    # add a block plot for each group in the stack
    ggalign(size = unit(1, "cm"), data = NULL) +
    geom_tile(aes(x = 1, fill = factor(.panel))) +
    scale_fill_brewer(palette = "Dark2", name = "Kmeans group") +
    scale_x_continuous(breaks = NULL, name = NULL) +
    # add a heatmap plot in the stack
    heat1 +
    # add another heatmap in the stack
    heat2 +
    # we move into the stack layout
    stack_active() +
    # add a point plot
    ggalign(data = expr$length, size = unit(2, "cm")) +
    geom_point(aes(x = value)) +
    labs(x = "length") +
    theme(
        panel.border = element_rect(fill = NA),
        axis.text.x = element_text(angle = -60, hjust = 0)
    ) +
    # add another heatmap
    heat3 &
    theme(
        plot.background = element_blank(),
        panel.background = element_blank(),
        legend.background = element_blank()
    )

在这里插入图片描述

教程2

mat <- read_example("measles.rds")
ggheatmap(mat, filling = FALSE) +
    geom_tile(aes(fill = value), color = "white") +
    scale_fill_gradientn(
        colours = c("white", "cornflowerblue", "yellow", "red"),
        values = scales::rescale(c(0, 800, 1000, 127000), c(0, 1))
    ) +
    theme(axis.text.x = element_text(angle = -60, hjust = 0)) +
    anno_right() +
    align_dendro(plot_dendrogram = FALSE) +
    anno_top(size = unit(2, "cm")) +
    ggalign(data = rowSums) +
    geom_bar(aes(y = value), fill = "#FFE200", stat = "identity") +
    scale_y_continuous(expand = expansion()) +
    ggtitle("Measles cases in US states 1930-2001\nVaccine introduced 1961") +
    theme(plot.title = element_text(hjust = 0.5)) +
    anno_right(size = unit(2, "cm")) +
    ggalign(data = rowSums) +
    geom_bar(aes(x = value),
        fill = "#FFE200", stat = "identity",
        orientation = "y"
    ) +
    scale_x_continuous(expand = expansion()) +
    theme(axis.text.x = element_text(angle = -60, hjust = 0))

在这里插入图片描述

参考

  • https://github.com/Yunuuuu/ggalign/tree/main
  • https://yunuuuu.github.io/ggalign-book/

http://www.kler.cn/a/537938.html

相关文章:

  • 青少年编程与数学 02-009 Django 5 Web 编程 03课题、项目结构
  • 洛谷算法1-3 暴力枚举
  • F - Building Roads S
  • MySQL第五次作业
  • Linux之kernel(1)系统基础理论(1)
  • cs106x-lecture2(上)(Autumn 2017)
  • 判断您的Mac当前使用的是Zsh还是Bash:echo $SHELL、echo $0
  • java基础4(黑马)
  • 语言月赛 202308【小粉兔做麻辣兔头】题解(AC)
  • TypeScript 中的元组:固定长度的数组
  • 论软件架构风格论文
  • 【C#】任务调度的实现原理与组件应用Quartz.Net
  • windows蓝牙驱动开发-蓝牙 LE 邻近感应配置文件
  • 小程序:如何暂时停用小程序?
  • 模板方法模式(Template)
  • 利用UNIAPP实现短视频上下滑动播放功能
  • #光子学小白#光子学与人工智能的跨界融合:从基础研究到产业应用 怎么学啊?
  • 悬链线的方程及其推导过程
  • 代理软件更改IP地址会影响网速吗
  • 5.5信息安全
  • Flutter完整开发实战详解(六、 深入Widget原理)
  • 图解BWT FM-Index算法
  • 如何在Excel内,完成excel到json的转换,excel另存为json,excel-to-json插件
  • mysql自连接 处理层次结构数据
  • 【CAPL实战】LIN调度表操作
  • 6.【BUUCTF】[极客大挑战 2019] Http(HTTP头伪造)