Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当保存在r中的矩阵中时,在网格中显示保存的GGPlot_R_Ggplot2_Cowplot - Fatal编程技术网

当保存在r中的矩阵中时,在网格中显示保存的GGPlot

当保存在r中的矩阵中时,在网格中显示保存的GGPlot,r,ggplot2,cowplot,R,Ggplot2,Cowplot,我使用循环创建了图,并将它们保存在矩阵中。现在,我想使用plot_grid或类似工具在网格中排列绘图。有没有一种简单的方法来调用保存的绘图矩阵?我希望生成的绘图网格与矩阵的布局相匹配 library(ggplot2) library(gridExtra) library(cowplot) # create and save the plots to the matrix plt <- vector('list', 10) plt <- matrix(plt, nrow = 5,

我使用循环创建了图,并将它们保存在矩阵中。现在,我想使用plot_grid或类似工具在网格中排列绘图。有没有一种简单的方法来调用保存的绘图矩阵?我希望生成的绘图网格与矩阵的布局相匹配

library(ggplot2)
library(gridExtra)
library(cowplot)


# create and save the plots to the matrix
plt <- vector('list', 10)
plt <- matrix(plt, nrow = 5, ncol = 2)

it = 1
while (it < 5){
  myX = runif(10)
  myY = runif(10)
  df = data.frame(myX,myY)
  plt[[it, 1]] = ggplot(data = df, aes(myX, myY)) + 
    geom_point(size = 2, color = "blue")
  plt[[it, 2]] = ggplot(data = df, aes(myX, myY)) + 
    geom_point(size = 2, color = "red")
  it = it + 1
}  

# display the plots in a grid that matches the matrix format
a1<- plt[[1,1]]
b1 <-plt[[1,2]]

a2<- plt[[2,1]]
b2 <-plt[[2,2]]

plot_grid(a1, b1, a2, b2, ncol = 2)
我上面所做的工作是有效的,但我必须将矩阵的每个元素分配给一个变量,然后在plot_grid命令中按顺序手动调用所有变量。我正在设法避免这种情况。我刚刚在这里展示了一个4的网格——在我真正的问题中,我有更多的情节

我尝试过使用plot_gridplt,ncol=2,这会导致无法将matrixarray类的对象转换为grob的错误。我还尝试了mylist=cplt[[1,1]]、plt[[1,2]]、plt[[2,1]]、plt[[2,2]]和plot_gridmylist,ncol=2,但得到了相同的错误


我还尝试基于使用do.callgrid.arrange、cplt、ncol=2,但未能实现该功能。

在矩阵中存储非原子对象可能会非常混乱。因此,首先,在普通列表中收集所需的绘图,然后在将列表传递到plot_grid时,确保通过plotlist=参数执行此操作

mylist = list(plt[[1,1]], plt[[1,2]], plt[[2,1]], plt[[2,2]])
plot_grid(plotlist=mylist, ncol=2)
如果要打印所有值,则只需转置列表,因为列表按列顺序存储,但默认情况下按行顺序打印

plot_grid(plotlist=t(plt), ncol=2)
如果您有一个简单的列表而不是矩阵列表,那么do.call就会起作用。在尝试打印时,可以使用辅助函数从列表中删除矩阵零件。比如说

undim <- function(x) {dim(x) <- NULL; x}
do.call("plot_grid", c(undim(plt), ncol = 2))

但在这里,plotlist=参数无疑是dg的更好方法。

将非原子对象存储在矩阵中可能会非常混乱。因此,首先,在普通列表中收集所需的绘图,然后在将列表传递到plot_grid时,确保通过plotlist=参数执行此操作

mylist = list(plt[[1,1]], plt[[1,2]], plt[[2,1]], plt[[2,2]])
plot_grid(plotlist=mylist, ncol=2)
如果要打印所有值,则只需转置列表,因为列表按列顺序存储,但默认情况下按行顺序打印

plot_grid(plotlist=t(plt), ncol=2)
如果您有一个简单的列表而不是矩阵列表,那么do.call就会起作用。在尝试打印时,可以使用辅助函数从列表中删除矩阵零件。比如说

undim <- function(x) {dim(x) <- NULL; x}
do.call("plot_grid", c(undim(plt), ncol = 2))

但在这里,plotlist=参数肯定是dg的更好方法。

与@MrFlick一致,我建议将绘图存储在一个列表中,可以通过参数plotlist轻松地传递到绘图网格

图书馆GGPLOT2 图书馆网格额外 图书馆考图 它=1
plt根据@MrFlick,我建议将绘图存储在一个列表中,该列表可以通过参数plotlist轻松地传递到绘图网格

图书馆GGPLOT2 图书馆网格额外 图书馆考图 它=1 plt