gpt4 book ai didi

r - 在 r 的一页上显示 ggplot 的多个图

转载 作者:行者123 更新时间:2023-12-02 08:41:54 26 4
gpt4 key购买 nike

我想在一个循环中制作多个 ggplot 并将它们显示在一个图上。

for ( i in 1:8) {
g <- ggplot(data=mtcars, aes(x=hp, y=wt))+
geom_point()
print(g)
}

我想将上面的图排列在一页上,4行2列。有谁知道这是怎么做到的吗?谢谢。

最佳答案

您可以将所有绘图保存在列表中,然后使用cowplot::plot_grid()gridExtra::marrangeGrob()来将它们放在一页或多页中

另请参阅:

library(tidyverse)

# create a list with a specific length
plot_lst <- vector("list", length = 8)

for (i in 1:8) {
g <- ggplot(data = mtcars, aes(x = hp, y = wt)) +
geom_point()
plot_lst[[i]] <- g
}

# Combine all plots
cowplot::plot_grid(plotlist = plot_lst, nrow = 4)

library(gridExtra)
ml1 <- marrangeGrob(plot_lst, nrow = 2, ncol = 2)
ml1

reprex package于2018年9月20日创建(v0.2.1.9000)

关于r - 在 r 的一页上显示 ggplot 的多个图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52436487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com