r - ggplot2 - 正确地将奇数个图排列成一个图形
问题描述
我有奇数个图要排列成一个图形,我希望显示最后一个图形的最后一行居中。
这里有一些示例数据:
library(ggplot2)
set.seed(99)
x_1 = data.frame(z = rnorm(100))
x_2 = data.frame(z = rnorm(100))
x_3 = data.frame(z = rnorm(100))
lst = list(x_1, x_2, x_3)
lst_p = list()
for (i in 1:length(lst)) {
lst_p[[i]] = ggplot(data=lst[[i]], aes(lst[[i]]$z)) +
geom_histogram() +
xlab("X LAB") +
ylab("Y LAB")
}
p_no_labels = lapply(lst_p, function(x) x + xlab("") + ylab(""))
title = cowplot::ggdraw() + cowplot::draw_label("test", size = 20)
p_grid = cowplot::plot_grid(plotlist = p_no_labels, ncol = 2)
print(cowplot::plot_grid(title, p_grid,
ncol = 1, rel_heights = c(0.05, 1, 0.05)))
我想要第三个图在图的中心。我正在使用cowplot
.
有什么建议吗?谢谢
解决方案
使用嵌套cowplot::plot_grid
s:
library(ggplot2)
set.seed(99)
x_1 = data.frame(z = rnorm(100))
x_2 = data.frame(z = rnorm(100))
x_3 = data.frame(z = rnorm(100))
lst = list(x_1, x_2, x_3)
lst_p = list()
for (i in 1:length(lst)) {
lst_p[[i]] = ggplot(data=lst[[i]], aes(lst[[i]]$z)) +
geom_histogram() +
xlab("X LAB") +
ylab("Y LAB")
}
p_no_labels = lapply(lst_p, function(x) x + xlab("") + ylab(""))
title = cowplot::ggdraw() + cowplot::draw_label("test", size = 20)
top_row = cowplot::plot_grid(p_no_labels[[1]], p_no_labels[[2]], ncol=2)
bottom_row = cowplot::plot_grid(NULL, p_no_labels[[2]], NULL, ncol=3, rel_widths=c(0.25,0.5,0.25))
cowplot::plot_grid(title, top_row, bottom_row, ncol=1, rel_heights=c(0.1,1,1))
推荐阅读
- php - 无法使用 PhpMailer 和 Codeigniter 通过 Outlook SMTP 发送电子邮件
- javascript - 打开一个新网页并自动输入登录详细信息并从 javascript 对象中获取数据?
- javascript - Nuxtjs 路由器破坏了基于 Java 脚本的仪表板组件
- excel - 在 Excel 宏中为证书从短到长格式化日期
- node.js - Hyperledger Caliper 中的基准错误代码 4 是什么
- r - 如何更改 `ggpubr::stat_regline_equation` 文本?
- swift - 如何将嵌套数据保存到 Firestore?
- c - Dietlibc、lowfat、opentracker - 针对替代 libc 进行编译
- autodesk-forge - Autodesk Forge Viewer 透明背景
- c++ - 架构 x86_64 c++ Visual Studio 代码的未定义符号。游戏蛇和梯子