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

embedded/2025/2/9 6:01:48/

在这里插入图片描述

文章目录

    • 介绍
    • 案例
    • 安装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 heatmapanno_top() +align_dendro() +# add a block for the heatmap columnggalign(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 stackanno_top(size = unit(4, "cm")) +# add box plot in the heatmap topggalign() +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-axisanno_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 groupsalign_kmeans(centers = 5L) +# add a block plot for each group in the stackggalign(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 stackheat1 +# add another heatmap in the stackheat2 +# we move into the stack layoutstack_active() +# add a point plotggalign(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 heatmapheat3 &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.ppmy.cn/embedded/160732.html

相关文章

Maven插件—flatten-maven-plugin:工程模块统一版本依赖

文章目录 前言一、认识flatten-maven-plugin插件二、如何使用flatten-maven-plugin插件&#xff1f;未使用flatten-maven-plugin插件之前的情况描述配置flatten-maven-plugin插件步骤1&#xff1a;最外层父模块安装插件&配置版本变量步骤2&#xff1a;各个自模块使用版本使…

2025年02月05日Github流行趋势

项目名称&#xff1a;OCRmyPDF 项目地址url&#xff1a;https://github.com/ocrmypdf/OCRmyPDF项目语言&#xff1a;Python历史star数&#xff1a;15872今日star数&#xff1a;157项目维护者&#xff1a;jbarlow83, fritz-hh, apps/dependabot, mawi12345, mara004项目简介&…

学习笔记十九:K8S生成pod过程

K8S生成pod过程 流程图具体生成过程用户提交 Pod 定义API Server 处理请求调度器分配节点&#xff08;Scheduling&#xff09;目标节点上的 Pod 创建网络配置状态上报与监控控制器管理&#xff08;Controller Manager&#xff09;就绪与服务发现 关键错误场景高级特性 流程图 具…

RocketMQ面试题:原理部分

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家&#xff0c;历代文学网&#xff08;PC端可以访问&#xff1a;https://literature.sinhy.com/#/?__c1000&#xff0c;移动端可微信小程序搜索“历代文学”&#xff09;总架构师&#xff0c;15年工作经验&#xff0c;精通Java编…

[每周一更]-(第133期):Go中MapReduce架构思想的使用场景

文章目录 **MapReduce 工作流程**Go 中使用 MapReduce 的实现方式&#xff1a;**Go MapReduce 的特点****哪些场景适合使用 MapReduce&#xff1f;**使用场景1. 数据聚合2. 数据过滤3. 数据排序4. 数据转换5. 数据去重6. 数据分组7. 数据统计8.**统计文本中单词出现次数****代码…

JUnit 5 中获取测试类、测试方法及属性的注解

JUnit 5 中获取测试类、测试方法及属性的注解 JUnit 5 提供了强大的扩展机制&#xff0c;允许通过 ExtensionContext 获取测试类、测试方法及其属性上的注解信息。以下是具体实现方法和示例&#xff1a; 一、核心 API ExtensionContext 提供测试执行的上下文信息&#xff0c;包…

Ollama 本地部署 体验 deepseek

下载安装ollama,选择模型 进行部署 # 管理员命令行 执行 ollama run deepseek-r1:70b浏览器访问http://ip:11434/ 返回 Ollama is runninghttp://ip:11434/v1/models 返回当前部署的模型数据 下载安装CherryStudio&#xff0c;本地对话UI 客户端 在设置中 修改API地址&#x…

A股level2高频数据分析20250205

A股level2高频数据分析20250205 通过Level2的逐笔成交与委托记录&#xff0c;这种高精度的毫秒级数据能够洞察诸多重要信息&#xff0c;包括庄家目的、误导性行为&#xff0c;使所有交易操作透明化。这对于分析高手的交易策略极为有益&#xff0c;对机器学习的研究也极具价值&…