简体   繁体   English

如何在 R 中的两列中打印大量 ggplot 图形而不将它们展平?

[英]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.

相关问题 如何使 R 中 ggplot 图形的 y 轴标签重叠 - How to make y-axis labels overlap for ggplot figures in R 如何在ggplot / R中制作这个直方图? - How can I make this histogram in ggplot / R? 如何将文本粘贴到 R 中 ggplot2 数字中的数字 y 轴刻度上? - How can I paste text onto numerical y-axis ticks in ggplot2 figures in R? R:两列并试图制作密度或箱线图。 我如何堆叠它们 - R: Two columns and trying to make a density or boxplot. How do I stack them 如何在 R 中找到一列的最高编号并打印该行的两列? - How to find the highest number of a column and print two columns of that row in R? 如何按不同的列比较 R 数据帧中的两行并对它们执行操作? - How can I compare two rows in R data frame by different columns and perform an operation on them? 如何在 R 中合并两组数据并在解析后将它们分别添加到单个列中? - How in R can I combine two groups of data and add them each into single columns after parsing? 如何确定 R 中 LARGE data.table 的两列中最长的公共子串 - How can I determine the longest common substring in two columns of a LARGE data.table in R 如何通过添加两列并为其之一赋予优先级来创建新列? - How can I make a new column by adding two columns with a priority given to one of them? 如何在R中合并两个数字 - How to merge two figures in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM