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

news/2025/2/9 5:37:38 标签: 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.niftyadmin.cn/n/5845586.html

相关文章

模板方法模式(Template)

一、模板方法的定义&#xff1a; 在操作中定义业务逻辑框架&#xff0c;包含业务逻辑的方法就是模板方法&#xff0c;模板方法允许子类在不改变原有业务逻辑的流程下&#xff0c;对某些步骤进行扩展和修改&#xff1b; 是一种基于继承的代码复用技术&#xff0c;是一种类行为…

Django开发入门 – 0.Django基本介绍

Django开发入门 – 0.Django基本介绍 A Brief Introduction to django By JacksonML 1. Django简介 1) 什么是Django? 依据其官网的一段解释&#xff1a; Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. …

pytest.fixture

pytest.fixture 是 pytest 测试框架中的一个非常强大的功能,它允许你在测试函数运行前后执行一些设置或清理代码。以下是关于 pytest.fixture 的详细介绍: 一、定义与用途 pytest.fixture 是一个装饰器,用于标记一个函数为 fixture。Fixture 函数中的代码可以在测试函数运…

基于 AI 智能名片 2+1 链动模式 S2B2C 商城小程序的线下门店同城流量运营策略研究

摘要&#xff1a;本文聚焦于线下门店在利用同城流量售卖产品过程中面临的机遇与挑战&#xff0c;深入探讨如何借助 AI 智能名片 21 链动模式 S2B2C 商城小程序优化运营策略。通过分析该模式与线下门店基于抖音平台开展业务的结合点&#xff0c;为线下门店在抖音流量红利下实现人…

Node.js 应用性能测试:Autocannon 的使用与 Docker 集成

在上一篇文章中&#xff0c;我们探讨了如何通过 PM2 和 Docker 的结合来高效管理和部署 Node.js 应用。本文将进一步介绍如何通过 autocannon 这一强大的性能测试工具&#xff0c;评估和优化 Node.js 应用的性能表现。无论是 API 服务、Web 应用&#xff0c;还是微服务架构&…

基于yolov11的阿尔兹海默症严重程度检测系统python源码+onnx模型+评估指标曲线+精美GUI界面

【算法介绍】 基于YOLOv11的阿尔兹海默症严重程度检测系统是一种创新的医疗辅助工具&#xff0c;旨在通过先进的计算机视觉技术提高阿尔兹海默症的早期诊断和病情监测效率。阿尔兹海默症是一种渐进性的神经退行性疾病&#xff0c;通常表现为认知障碍、记忆丧失和语言障碍等症状…

部署open webui 调用ollama启动的deepseek

以下是 部署Open WebUI并调用Ollama的deepseek-70b模型 的详细步骤&#xff0c;重点讲解部署和配置过程&#xff1a; 一、部署 Open WebUI 1. 安装Docker&#xff08;如已安装可跳过&#xff09; # 自动安装Docker curl -fsSL https://get.docker.com | sh # 启动Docker服务 …

MyBatis-Plus与PageHelper的jsqlparser库冲突问题

背景介绍 在最近的一个项目中&#xff0c;我们决定将Spring Boot升级至3.x版本&#xff0c;并同时升级了MyBatis-Plus到最新版本&#xff08;3.5.7&#xff09;。然而&#xff0c;在完成这些升级之后&#xff0c;我们遇到了一个问题&#xff1a;分页插件PageHelper无法正常工作…