0

I have a data set (A2) which looks like this;

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

and I am using the following code to generate a line graph;

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)

at the moment I am replicating the code for each different region which is making my script very long. I was wondering if there is a way to loop the code to go through each of the regions and generate a plot for each?

I have tried using the answers available from this previous question Loop through a series of qplots but when I use this code it returns a 'non-numeric argument to binary operator' error.

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)
}
3
  • check out here r-bloggers.com/ggplot2-graphics-in-a-loop Commented Oct 2, 2015 at 11:09
  • names(A2$Region) will return NULL. You probably want unique there. There is also a , after A2 where I think you want to subset. Commented Oct 2, 2015 at 11:10
  • And you re missing a bracket for print Commented Oct 2, 2015 at 11:12

1 Answer 1

2
for(Var in unique(A2$Region)){
  print(
    ggplot(
      subset(A2, Region == 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)
  )
}

or using the plyr package

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 makes it easy to split the dataset along multiple variables.

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)
})
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way of putting that loop within another loop. I want to have the same plots for each species but still by each region. Can you have two back to back 'for' satements?
I am struggling with generating appropriate titles for each looped plot; with the first answer you gave me I have been using 'ggtitle(paste(Var,"landings by gear time-series")) which worked fine however I can't get a similar line to work for the plyr multiple variables code. I have tried a number of different arrangements but I am getting the Error: object 'Species.Code' not found or it is just giving them all the same region and species name.
use something like ggtitle(paste("fixed title", x$Species[1]))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.