[英]How can I make a large number of ggplot figures print in two columns in R without flattening them?
For example, if you run the following, you will see that grid.arrange squeezes all 20 ggplot plots into one figure making them unreadable.例如,如果您运行以下命令,您将看到 grid.arrange 将所有 20 个 ggplot 图压缩成一个图形,使它们无法读取。
Is there a way to create a tall figure that can contain dozens of these plots, or is there an easy way to get them to print four at a time 2 by 2 via grid.arrange output until there's no more in the list?有没有办法创建一个可以包含几十个这样的图的高大数字,或者有没有一种简单的方法让它们通过 grid.arrange 输出一次 2 x 2 打印四个,直到列表中没有更多?
The number of figures in the list may or may not be divisible by four.列表中的数字可能被四整除,也可能不能被四整除。 For example, if I had 9 plots in the list, it would need to print a 2 by 2 image, another 2 by 2 image, and then the last one.
例如,如果我在列表中有 9 个图,它需要打印一个 2 x 2 的图像,另一个 2 x 2 的图像,然后是最后一个。
library(gridExtra)
library(ggplot2)
#Create the dataframe.
df<-data.frame(matrix(rexp(200), 10))
colnames(df)<-c("aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj")
#Create the ggplots in a loop.
hlist<-list()
for(i in 1:ncol(df)){
hplot<-eval(substitute(ggplot(df[i], aes(x=df[,i])) + geom_histogram(),list(i = i)))
hlist[[i]]<-hplot
}
#Print the plots.
grid.arrange(grobs = hlist, ncol = 2)
Try this approach using patchwork
to create a 2x2 plots creating indexes after storing all plots in a new list.在将所有绘图存储在新列表中后,尝试使用
patchwork
创建一个 2x2 绘图创建索引的方法。 With index vectors you can set a loop and use wrap_plots()
to arrange them.使用索引向量,您可以设置一个循环并使用
wrap_plots()
来排列它们。 Here the code:这里的代码:
library(ggplot2)
library(patchwork)
#Create the dataframe.
df<-data.frame(matrix(rexp(200), 10))
colnames(df)<-c("aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj")
#Create the ggplots in a loop.
hlist<-list()
for(i in 1:ncol(df)){
hplot<-eval(substitute(ggplot(df[i], aes(x=df[,i])) + geom_histogram(),list(i = i)))
hlist[[i]]<-hplot
}
#Print the plots.
vec <- seq(1,length(hlist),by=4)
vec2 <- seq(4,length(hlist),by=4)
#New list
hlist2 <- list()
#Loop
for(i in 1:length(vec))
{
hlist2[[i]] <- wrap_plots(hlist[vec[i]:vec2[i]],ncol = 2)
}
#Arrange
finalplot <- wrap_plots(hlist2,ncol = 2)
Output:输出:
All the 2x2 plots are stored in hlist2
.所有 2x2 图都存储在
hlist2
。
I found the solution for creating a list of plot pages then printing each page using marrangeGrob:我找到了创建绘图页面列表然后使用 marrangeGrob 打印每个页面的解决方案:
library(gridExtra)
library(ggplot2)
#Create the dataframe.
df<-data.frame(matrix(rexp(200), 10))
colnames(df)<-c("aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj")
#Create the ggplots in a loop.
hlist<-list()
for(i in 1:ncol(df)){
hplot<-eval(substitute(ggplot(df[i], aes(x=df[,i])) + geom_histogram(),list(i = i)))
hlist[[i]]<-hplot
}
#Uses marrangeGrob to first store them in multiple pages.
glist<-marrangeGrob(hlist, nrow=2, ncol=2)
#Print each page one at a time until through all pages.
for(i in 1:length(glist)){grid.arrange(grobs = glist[i], ncol = 1)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.