R 从数据帧列表打印列

R 从数据帧列表打印列,r,lapply,R,Lapply,我试图将一些日期分为几个季节,然后结合每个季节的“日期和幅度”月度图 head(dat) Date Amplitude 1 2009-09-13 -2.6966667 2 2009-09-14 -0.7264583 3 2009-09-15 -2.1823333 以下是我为情节标题创建季节和年月的代码: x <- as.Date(dat$Date, format="%Y/%m/%d") x <- as.Date(x, origin="2009-09-13") x <

我试图将一些日期分为几个季节,然后结合每个季节的“日期和幅度”月度图

head(dat)
    Date  Amplitude
1 2009-09-13 -2.6966667
2 2009-09-14 -0.7264583
3 2009-09-15 -2.1823333
以下是我为情节标题创建季节和年月的代码:

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

x这里有一个最小的示例,我相信它可以重新创建您想要的输出

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)
)

df1所以问题是您的
ggplot
调用工作不正常?ggplot调用工作正常-我想按照数据帧列表排序,将每个季节传递给ggplot调用,而无需手动输入。i、 e.按季节分割数据,按$YearMonth分割季节,传递给ggplot。一应俱全。这有意义吗?也许,你有一个数据帧列表,每个数据帧都是一个季节?你想要每个季节的网格图(每个面板是YearMonth),还是想要每个YearMonth完全独立的图表?我想要每个季节的网格图(3个图表),每个面板是YearMonth。当我手动传递一个季节(即df=季节)时,ggplot调用就实现了这一点,但是我想循环整个过程,从split(dat,dat$DateQ)到将季节$YearMonth传递给ggplot。这将起作用,谢谢!您能否快速解释一下为什么将ggplot函数指定给lappy()中的变量“p”。您可以保存
ggplot
对象,以便以后决定打印它们或保存或添加图层。但是我们不必这样做-无论哪种方式,
lappy()
的结果都将是
ggplot
对象的列表,我将其命名为
myplots
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)
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)
)