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:
这是我为剧情标题创建季节和YearMonth的代码:
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
我使用拆分为每个季节创建数据框,然后使用for循环命名数据框
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手动创建所需的月度图
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'.
我想使用for循环或apply()系列来对季节列表进行排序,为每个数据框选择Date和Amplitude列,并按'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!
我遇到的问题可能与语法有关;我是R的新手和一般的编程。但是我花了最近几天在论坛上搜索,我希望在这一点上有一些方向。谢谢!
1 个解决方案
#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.
lapply用于访问数据帧列表,然后facet_wrap用于为每个数据帧创建绘图网格。
#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.
lapply用于访问数据帧列表,然后facet_wrap用于为每个数据帧创建绘图网格。