1

I'm trying to subset some dates into season then combine monthly plots of 'date and amplitude' for each season.

head(dat)
    Date  Amplitude
1 2009-09-13 -2.6966667
2 2009-09-14 -0.7264583
3 2009-09-15 -2.1823333

Here's my code to create seasons and YearMonth for plot titles:

x <- as.Date(dat$Date, format="%Y/%m/%d")
x <- as.Date(x, origin="2009-09-13")
x <- cut(x, breaks="quarter")
labs <- paste(substr(levels(x),1,4),"/",1:4, sep="")
dat$DateQ <- factor(x, labels=labs)
dat$YearMonth <- format(dat$Date, "%Y/%m")
head(dat)
Date  Amplitude  DateQ YearMonth
1 2009-09-13 -2.6966667 2009/1   2009/09
2 2009-09-14 -0.7264583 2009/1   2009/09

I used a split to create data frames for each season and then named the data frames with a for loop

dat_split_season <- split(dat, dat$DateQ)
new_names <- c("Summer_2009", "Fall_2009", "Winter_2010", "Spring_2010", 
          "Summer_2010", "Fall_2010", "Winter_2011",
          "Spring_2011", "Summer_2011", "Fall_2011",
          "Winter_2012", "Spring_2012", "Summer_2012")
for (i in 1:length(dat_split_season)) {
assign(new_names[i], dat_split_season[[i]])
}

I can create the desired monthly plots by season manually by changing df

df <- Winter_2012
graphs <- lapply(split(df,df$YearMonth),
   function(gg) ggplot(gg, aes(x=Date, y=Amplitude)) +
     geom_point() + geom_line() + ylim(-10, 10) +
     ggtitle(paste(gg$YearMonth)) + theme_bw())
do.call("grid.arrange", graphs)

I would like to use a for loop or the apply() family to sequence through the seasons list, selecting the Date and Amplitude columns for each data frame and plotting by 'YearMonth'.

The issue I'm having is probably syntax related; I'm new to R and programming in general. But I have spent the last few days scouring the forum and I would appreciate some direction at this point. Thanks!

4
  • so the problem is that your ggplot call isn't working correctly? Commented Nov 10, 2014 at 15:50
  • ggplot call works fine - I want to sequence along the list of data frames, passing each season to the ggplot call without inputting manually. i.e. split dat by season, split season by $YearMonth, pass to ggplot. All in one. Does that make sense? Commented Nov 10, 2014 at 16:03
  • Maybe, so you have a list of dataframes, each of which is a season? Do you want a grid of plots for each season (each panel being YearMonth), or do you want entirely independent plots for each YearMonth ? Commented Nov 10, 2014 at 16:19
  • I want a grid of plots (3 plots) for each season, each panel being YearMonth. The ggplot call achieves this when I pass it a season manually (i.e. df = season), but I want to loop the whole process from split(dat, dat$DateQ) through to passing season$YearMonth to ggplot. Commented Nov 10, 2014 at 16:26

1 Answer 1

1

Here's a minimal example which I believe recreates your desired output

df1<-data.frame(YearMonth=c(rep('Aug_2012',10),rep('Sep_2012',10),rep('Oct_2012',10)), value=rnorm(30),t=rep(1:10,3) )
df2<-data.frame(YearMonth=c(rep('Aug_2012',10),rep('Sep_2012',10),rep('Oct_2012',10)), value=rnorm(30),t=rep(1:10,3) )
df_list<-list(df1,df2)

myplots<-lapply(df_list,function(x)
p<-ggplot(x,aes(x=t,y=value,group=factor(YearMonth),color=factor(YearMonth))) + geom_line() + facet_wrap(~YearMonth)
)

The lapply is used to access the list of dataframes, and then facet_wrap is used to create the grid of plots for each dataframe.

Sign up to request clarification or add additional context in comments.

2 Comments

This will work, thank you! Could you quickly explain why you assigned the ggplot function to a variable 'p' in the lapply()
You can save ggplot objects, so you can decide later to print them or save or add layers to them. We don't have to do this though - Either way, the result of lapply() will be a list of ggplot objects, which I named myplots.

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.