您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# R语言中ggpubr包的ggarrange()函数怎么用
## 一、ggpubr包与ggarrange()简介
ggpubr是基于ggplot2的一个扩展包,由Alboukadel Kassambara开发,专门为科研人员提供更简便的统计图形绘制方案。其中`ggarrange()`函数是该包中用于多图排列的核心工具,能够高效地将多个ggplot对象组合成复杂的图形布局。
### 主要特点
1. **语法简洁**:相比ggplot2的`grid.arrange()`或`patchwork`包更易上手
2. **灵活布局**:支持自定义行列数、对齐方式和间距
3. **标签添加**:可自动添加A/B/C等子图标签
4. **混合图形**:支持ggplot图形与基础图形混合排列
## 二、安装与基础用法
### 1. 安装ggpubr包
```r
install.packages("ggpubr")
library(ggpubr)
ggarrange(
...,
plots = list(),
ncol = NULL,
nrow = NULL,
labels = NULL,
widths = NULL,
heights = NULL,
align = c("none", "h", "v", "hv"),
common.legend = FALSE,
legend = c("top", "bottom", "left", "right", "none")
)
首先生成两个示例图形:
library(ggplot2)
p1 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p2 <- ggplot(mtcars, aes(factor(cyl), hp)) + geom_boxplot()
最简单的多图排列:
ggarrange(p1, p2) # 默认横向排列
ggarrange(p1, p2, ncol = 1) # 纵向排列
ggarrange(p1, p2, p1, p2, ncol = 2, nrow = 2)
ggarrange(p1, p2, widths = c(2, 1)) # 第一列宽度是第二列的2倍
ggarrange(p1, p2, align = "v") # 垂直对齐坐标轴
p3 <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) + geom_point()
p4 <- ggplot(mtcars, aes(hp, wt, color = factor(cyl))) + geom_point()
ggarrange(p3, p4, common.legend = TRUE, legend = "bottom")
支持自动标签和自定义标签:
ggarrange(p1, p2,
labels = c("A: 散点图", "B: 箱线图"),
font.label = list(size = 12, color = "red"))
通过嵌套ggarrange实现复杂布局:
left_panel <- ggarrange(p1, p2, ncol = 1)
right_panel <- ggplot(mtcars, aes(factor(gear), mpg)) + geom_violin()
ggarrange(left_panel, right_panel, widths = c(1, 2))
# 创建基础图形
par(mar = c(4,4,1,1))
hist(mtcars$mpg)
base_plot <- recordPlot() # 保存基础图形
# 混合排列
ggarrange(p1, ggpubr::as_ggplot(base_plot))
# 创建汇总表格
df_summary <- data.frame(
Variable = c("mpg", "wt"),
Mean = c(mean(mtcars$mpg), mean(mtcars$wt))
)
table_plot <- ggpubr::ggtexttable(df_summary, rows = NULL)
ggarrange(p1, table_plot, ncol = 1, heights = c(2, 1))
当图形坐标轴刻度不一致时:
ggarrange(p1, p2, align = "hv") # 强制对齐
通过plot.margin
调整:
p1 + theme(plot.margin = margin(1,1,1,1, "cm"))
使用ggsave()
时指定dpi:
final_plot <- ggarrange(p1, p2)
ggsave("output.tiff", final_plot, dpi = 300, width = 10, height = 6)
特性 | ggarrange | grid.arrange | patchwork |
---|---|---|---|
语法简洁性 | ★★★★★ | ★★★☆☆ | ★★★★☆ |
自动对齐 | 支持 | 不支持 | 支持 |
子图标签 | 自动生成 | 需手动添加 | 需手动添加 |
布局灵活性 | ★★★★☆ | ★★★☆☆ | ★★★★★ |
图例共享 | 支持 | 不支持 | 支持 |
# 准备4个分析图形
p1 <- ggboxplot(mtcars, "cyl", "mpg", color = "cyl")
p2 <- ggscatter(mtcars, "wt", "mpg", add = "reg.line")
p3 <- ggdensity(mtcars, "mpg", fill = "gray")
p4 <- ggbarplot(table(mtcars$gear))
# 创建底部注释
caption <- "图1. 汽车数据集分析结果\n(n = 32)"
# 组合图形
final_plot <- ggarrange(
ggarrange(p1, p2, ncol = 2, labels = c("A", "B")),
ggarrange(p3, p4, ncol = 2, labels = c("C", "D")),
nrow = 2,
bottom = text_grob(caption, color = "blue", hjust = 1, x = 1)
)
# 导出图形
ggsave("research_plot.png", final_plot, width = 10, height = 8)
ggpubr包的ggarrange()
函数为R用户提供了:
1. 比原生ggplot2更简便的多图排版方案
2. 丰富的布局控制选项
3. 专业的科研图形输出能力
4. 与其他ggpubr函数的无缝集成
通过掌握本文介绍的各类参数配置和技巧,读者可以轻松实现期刊论文级别的复杂图形排版需求。建议在实际工作中多结合具体数据特点尝试不同的参数组合,以达到最佳的可视化效果。 “`
注:本文实际约2300字,包含了代码示例、参数详解、对比表格和实用案例等完整内容,采用Markdown格式编写,可直接用于技术文档发布。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。