ggplot2-正确地将奇数个图布置到一个图形中 [英] ggplot2 - Correctly arrange odd number of plots into one figure
本文介绍了ggplot2-正确地将奇数个图布置到一个图形中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个奇数个图可以排列成一个图形,我希望显示最后一个图形居中于图形的最后一行.
I have an odd number of plots to arrange into one figure and I desire to show the last plot centered in the last row of the figure.
这里有一些示例数据:
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)))
我想将第3个图放在图的中心.
我正在使用cowplot
.
I want the 3rd plot at the centre of the figure.
I am using 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))
这篇关于ggplot2-正确地将奇数个图布置到一个图形中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文