Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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
保存在loop函数中制作的grobs_R_Function_For Loop_Ggplot2_Gridextra - Fatal编程技术网

保存在loop函数中制作的grobs

保存在loop函数中制作的grobs,r,function,for-loop,ggplot2,gridextra,R,Function,For Loop,Ggplot2,Gridextra,我做了这个长函数,以一个grob图(3个ggplots)结束。然而,当我在一个循环中使用这个函数,并试图将所有结果放入一个超级图形中时,我遇到了问题。我得到了一个数字,有很多的grob,但它总是相同的grob(第一个) 为了说明我的问题,我做了以下简化示例: library(ggplot2) library(gridExtra) data(iris) #the function multi.plot=function(data,heading){ p1=ggplot(data,aes(x

我做了这个长函数,以一个grob图(3个ggplots)结束。然而,当我在一个循环中使用这个函数,并试图将所有结果放入一个超级图形中时,我遇到了问题。我得到了一个数字,有很多的grob,但它总是相同的grob(第一个)

为了说明我的问题,我做了以下简化示例:

library(ggplot2)
library(gridExtra)

data(iris)

#the function
multi.plot=function(data,heading){
  p1=ggplot(data,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+ggtitle(heading)
  p2=ggplot(data,aes(x=Petal.Length,y=Sepal.Width))+geom_point()
  p3=ggplot(data,aes(x=Sepal.Length,y=Petal.Width))+geom_point()
  grob1=arrangeGrob(p1,ncol=2)
  grob2=arrangeGrob(p3,p2,ncol=2)
  grob3=arrangeGrob(grob1,grob2)
  # grob3  / return(grob3)  / print(grob3)   => all tried but non of them helps
}

# the loop
list.grob=list()
for(i in unique(iris$Species)){
  select=iris[iris$Species==i,]
  multi.plot(data=select,heading=i)
  list.grob[[which(unique(iris$Species)==i)]]=grob3  
}

# the final figure
png(file="superplot.png")
do.call("grid.arrange", list.grob)  
dev.off()
那么,如何使用函数和循环生成一个包含大量Grob的图形呢?此外,我的标题(“标题”)也没有按应有的样子出现


谢谢你的建议

此处无需对循环使用
。我的意思是最好按物种分组使用
by
,例如:

by(iris,iris$Species,
   function(select)
     multi.plot(data=select,heading=unique(select$Species)))
此外,还应简化多重绘图功能:

#the function
multi.plot=function(data,heading){
  p1=ggplot(data,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+ggtitle(heading)
  p2=ggplot(data,aes(x=Petal.Length,y=Sepal.Width))+geom_point()
  p3=ggplot(data,aes(x=Sepal.Length,y=Petal.Width))+geom_point()
  arrangeGrob(p1,arrangeGrob(p1,p2,ncol=2))
}

此处无需对
循环使用
。我的意思是最好按物种分组使用
by
,例如:

by(iris,iris$Species,
   function(select)
     multi.plot(data=select,heading=unique(select$Species)))
此外,还应简化多重绘图功能:

#the function
multi.plot=function(data,heading){
  p1=ggplot(data,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+ggtitle(heading)
  p2=ggplot(data,aes(x=Petal.Length,y=Sepal.Width))+geom_point()
  p3=ggplot(data,aes(x=Sepal.Length,y=Petal.Width))+geom_point()
  arrangeGrob(p1,arrangeGrob(p1,p2,ncol=2))
}

此处无需对
循环使用
。我的意思是最好按物种分组使用
by
,例如:

by(iris,iris$Species,
   function(select)
     multi.plot(data=select,heading=unique(select$Species)))
此外,还应简化多重绘图功能:

#the function
multi.plot=function(data,heading){
  p1=ggplot(data,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+ggtitle(heading)
  p2=ggplot(data,aes(x=Petal.Length,y=Sepal.Width))+geom_point()
  p3=ggplot(data,aes(x=Sepal.Length,y=Petal.Width))+geom_point()
  arrangeGrob(p1,arrangeGrob(p1,p2,ncol=2))
}

此处无需对
循环使用
。我的意思是最好按物种分组使用
by
,例如:

by(iris,iris$Species,
   function(select)
     multi.plot(data=select,heading=unique(select$Species)))
此外,还应简化多重绘图功能:

#the function
multi.plot=function(data,heading){
  p1=ggplot(data,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+ggtitle(heading)
  p2=ggplot(data,aes(x=Petal.Length,y=Sepal.Width))+geom_point()
  p3=ggplot(data,aes(x=Sepal.Length,y=Petal.Width))+geom_point()
  arrangeGrob(p1,arrangeGrob(p1,p2,ncol=2))
}

根据你的技巧,我将继续:

library(ggplot2)
library(gridExtra)
data(iris)
功能

multi.plot=function(data=iris, heading="virginica"){
        p1=ggplot(data,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+ ggtitle(heading)
        p2=ggplot(data,aes(x=Petal.Length,y=Sepal.Width))+geom_point()+ ggtitle(heading)
        p3=ggplot(data,aes(x=Sepal.Length,y=Petal.Width))+geom_point()+ ggtitle(heading)
        grob_all=arrangeGrob(p1, p2, p3, ncol=3)
        return(grob_all)}`

grob_test <- multi.plot()

根据你的技巧,我将继续:

library(ggplot2)
library(gridExtra)
data(iris)
功能

multi.plot=function(data=iris, heading="virginica"){
        p1=ggplot(data,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+ ggtitle(heading)
        p2=ggplot(data,aes(x=Petal.Length,y=Sepal.Width))+geom_point()+ ggtitle(heading)
        p3=ggplot(data,aes(x=Sepal.Length,y=Petal.Width))+geom_point()+ ggtitle(heading)
        grob_all=arrangeGrob(p1, p2, p3, ncol=3)
        return(grob_all)}`

grob_test <- multi.plot()

根据你的技巧,我将继续:

library(ggplot2)
library(gridExtra)
data(iris)
功能

multi.plot=function(data=iris, heading="virginica"){
        p1=ggplot(data,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+ ggtitle(heading)
        p2=ggplot(data,aes(x=Petal.Length,y=Sepal.Width))+geom_point()+ ggtitle(heading)
        p3=ggplot(data,aes(x=Sepal.Length,y=Petal.Width))+geom_point()+ ggtitle(heading)
        grob_all=arrangeGrob(p1, p2, p3, ncol=3)
        return(grob_all)}`

grob_test <- multi.plot()

根据你的技巧,我将继续:

library(ggplot2)
library(gridExtra)
data(iris)
功能

multi.plot=function(data=iris, heading="virginica"){
        p1=ggplot(data,aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+ ggtitle(heading)
        p2=ggplot(data,aes(x=Petal.Length,y=Sepal.Width))+geom_point()+ ggtitle(heading)
        p3=ggplot(data,aes(x=Sepal.Length,y=Petal.Width))+geom_point()+ ggtitle(heading)
        grob_all=arrangeGrob(p1, p2, p3, ncol=3)
        return(grob_all)}`

grob_test <- multi.plot()

谢谢你的回答,这个例子很有用。但不幸的是,在我真正的问题中,我在列上使用for循环,而不是在行的因子上使用for循环。我能做些类似的事情吗?你可以重塑数据,使列成为一个因子的值(即从宽到长)。谢谢你的回答,在这个例子中效果很好。但不幸的是,在我真正的问题中,我在列上使用for循环,而不是在行的因子上使用for循环。我能做些类似的事情吗?你可以重塑数据,使列成为一个因子的值(即从宽到长)。谢谢你的回答,在这个例子中效果很好。但不幸的是,在我真正的问题中,我在列上使用for循环,而不是在行的因子上使用for循环。我能做些类似的事情吗?你可以重塑数据,使列成为一个因子的值(即从宽到长)。谢谢你的回答,在这个例子中效果很好。但不幸的是,在我真正的问题中,我在列上使用for循环,而不是在行的因子上使用for循环。我能做些类似的事情吗?你可以改变数据的形状,使列成为因子的值(即从宽到长)