Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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/9/loops/2.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_Loops_Ggplot2 - Fatal编程技术网

如何在r中循环ggplot图

如何在r中循环ggplot图,r,loops,ggplot2,R,Loops,Ggplot2,我有一个数据集(A2),看起来像这样 Year Gear Region Landings.t 1975 Creel Clyde 3.456 1976 Creel Clyde 20.531 1977 Creel Clyde 56.241 1978 Creel Clyde 43.761 1975 Creel Shetland 3.456 1976 Creel S

我有一个数据集(A2),看起来像这样

Year    Gear      Region    Landings.t
1975    Creel     Clyde     3.456
1976    Creel     Clyde     20.531
1977    Creel     Clyde     56.241
1978    Creel     Clyde     43.761
1975    Creel     Shetland  3.456
1976    Creel     Shetland  10.531
1977    Creel     Shetland  46.241
1978    Creel     Shetland  33.761
我使用下面的代码生成一个折线图

ggplot(subset(A2,Region=="Clyde"),aes(x=Year,y=Landings.t,colour=Gear,group=Gear))+
  geom_line()+
  facet_grid(Gear~.,scales='free_y')+
  ggtitle("CLYDE LANDINGS BY GEAR TIME-SERIES")+
  theme(panel.background=element_rect(fill='white',colour='black'))+
  geom_vline(xintercept=1984)
目前,我正在为每个不同的区域复制代码,这使得我的脚本非常长。我想知道是否有一种方法可以循环代码,遍历每个区域并为每个区域生成一个绘图

我尝试过使用前一个问题的答案,但当我使用这段代码时,它返回一个“非数值参数到二进制运算符”错误

for(Var in names(A2$Region)){
print(ggplot(A2,[,Var],aes(x=Year,y=Landings.t,colour=Gear,group=Gear))+
geom_line()+
facet_grid(Gear~.,scales='free_y')+
ggtitle("CLYDE LANDINGS BY GEAR TIME-SERIES")+
theme(panel.background=element_rect(fill='white',colour='black'))+
geom_vline(xintercept=1984)
}
或使用
plyr
软件包

library(plyr)
dlply(A2, ~Region, function(x){
    ggplot(
      x,
      aes(x = Year, y = Landings.t, colour = Gear, group = Gear)
    )+
    geom_line() +
    facet_grid(Gear ~ ., scales = 'free_y') +
    ggtitle("CLYDE LANDINGS BY GEAR TIME-SERIES") +
    theme(panel.background = element_rect(fill = 'white', colour = 'black'))+
    geom_vline(xintercept = 1984)
})
plyr
可以轻松地将数据集沿多个变量拆分

dlply(A2, ~Region + Species, function(x){
    ggplot(
      x,
      aes(x = Year, y = Landings.t, colour = Gear, group = Gear)
    )+
    geom_line() +
    facet_grid(Gear ~ ., scales = 'free_y') +
    ggtitle("CLYDE LANDINGS BY GEAR TIME-SERIES") +
    theme(panel.background = element_rect(fill = 'white', colour = 'black'))+
    geom_vline(xintercept = 1984)
})

在这里签出
names(A2$Region)
将返回
NULL
。您可能需要
唯一的
。还有一个
,在
A2
之后,我认为您需要子集。您缺少了
打印
的括号。是否有办法将该循环放入另一个循环中。我希望每个物种都有相同的地块,但每个区域都有。你能有两个背对背的“for”语句吗?我正在努力为每个循环的情节生成合适的标题;根据您给我的第一个答案,我一直在使用“ggtitle”(粘贴(Var,“起落架时间序列的着陆”)),它运行良好,但是我无法获得类似的行用于plyr多变量代码。我尝试了许多不同的安排,但我得到的错误是:找不到对象'Species.Code',或者它只是给了它们相同的区域和物种名称。使用类似于ggtitle(粘贴(“固定标题”,x$Species[1]))的东西。
dlply(A2, ~Region + Species, function(x){
    ggplot(
      x,
      aes(x = Year, y = Landings.t, colour = Gear, group = Gear)
    )+
    geom_line() +
    facet_grid(Gear ~ ., scales = 'free_y') +
    ggtitle("CLYDE LANDINGS BY GEAR TIME-SERIES") +
    theme(panel.background = element_rect(fill = 'white', colour = 'black'))+
    geom_vline(xintercept = 1984)
})