使用Mapply将多个R ggplots ecdf参数页保存到pdf文件中

时间:2019-03-15 18:10:04

标签: r pdf ggplot2 mapply ecdf

我将变量的经验CDF与3个理论CDF进行比较。我对150个变量执行此操作,并希望将结果打印在一个PDF文件中,每页包含4个图表。我不使用循环,而是使用mapply。理想情况下,我可以使用par(mfrow = c(2,2)),但我认为这仅适用于R基础对象,而不适用于ggplot。我查看了gridExtra软件包here,但没有找到继续的方法。

library(evd)
library(MASS)
library(fitdistrplus)
library(actuar)
library(ADGofTest)
library (extRemes)
library (lmom)
library(gridExtra)
library(ggplot2)

var1<-rt(10000, df=1)
var2<-rt(10000, df=1)
var3<-rt(10000, df=1)
var4<-rt(10000, df=1)
df<-data.frame(var1,var2, var3, var4)  
colnames(df)<-c("var1", "var2", "var3", "var4")  

df<-data.frame(var1,var2, var3, var4)  
colnames(df)<-c("var1", "var2", "var3", "var4")  

pdf()
par(mfrow=c(2,2))

myFUN<-function(x, Name){
  empi<-na.omit(x)
  empi<-empi[which(empi>0)] 

  # Theoretical Pareto random series
  par.par<-fitdist(empi,  "pareto", start=list(shape = 1, scale = 500))
  shape.par<-par.par$estimate[1]
  scale.par<-par.par$estimate[2]
  x.par<-rpareto(NROW(empi), shape.par,scale.par)

  # Theoretical Weibull random series
  par.wei<-fitdist(empi, "weibull")
  shape.wei<-par.wei$estimate[1]
  scale.wei<-par.wei$estimate[2]
  x.wei<-rweibull(NROW(empi), shape.wei,scale.wei)

  # Theoretical GEV random series
  # Fittig EVD using the "extRemes" package (can't get it with fitdist)
  par.gev <- fevd(empi,type =("GEV"),method=("Lmoments")) 
  loc.gev<-par.gev$results[1]
  shape.gev<-par.gev$results[3]
  scale.gev<-par.gev$results[2]
  x.gev<-rgev(NROW(empi), loc=loc.gev, scale=scale.gev, shape=shape.gev)


  # Create dataframe for using with ggplot+stat_ecdf
  df<-data.frame(cbind(empi,rep("Empirical",times=NROW(empi))))
  colnames(df)<-c("X","distr")
  dfpar<-data.frame(cbind(x.par,rep("Pareto",times=NROW(x.par))))
  colnames(dfpar)<-c("X","distr")
  dfwei<-data.frame(cbind(x.wei,rep("Weibull",times=NROW(x.wei))))
  colnames(dfwei)<-c("X","distr")
  dfgev<-data.frame(cbind(x.gev,rep("GEV",times=NROW(x.gev))))
  colnames(dfgev)<-c("X","distr")
  df<-rbind(df,dfpar)
  df<-rbind(df,dfwei)
  df<-rbind(df,dfgev)
  df$X<-as.numeric(levels(df$X))[df$X] 

  g<-ggplot(df, aes(X, colour = distr, linetype = distr)) + stat_ecdf(size=1)+theme_classic() +
    scale_x_continuous(trans = 'log10')+scale_y_continuous(trans = 'log10') +
    xlab("Daily returns")+ylab("CDFs") + ggtitle(Name) + theme(plot.title = element_text(hjust = 0.5)) +
    theme(legend.position = c(0.85, 0.25), legend.text=element_text(size=12), legend.title=element_blank())

  print(g)

}

allgraph<-mapply(myFUN, df, names(df), SIMPLIFY = FALSE)

dev.off()

1 个答案:

答案 0 :(得分:1)

按照@bdemarest的建议,我返回到函数gridExtra::marrangeGrob,并找到了一种方法:

library(evd)
library(MASS)
library(fitdistrplus)
library(actuar)
library(ADGofTest)
library (extRemes)
library (lmom)
library(gridExtra)
library(ggplot2)    

var1<-rt(10000, df=1)
var2<-rt(10000, df=1)
var3<-rt(10000, df=1)
var4<-rt(10000, df=1)
df<-data.frame(var1,var2, var3, var4)  
colnames(df)<-c("var1", "var2", "var3", "var4")  

df<-data.frame(var1,var2, var3, var4)  
colnames(df)<-c("var1", "var2", "var3", "var4")  

myFUN<-function(x, Name){
  empi<-na.omit(x)
  empi<-empi[which(empi>0)] 

  # Theoretical Pareto random series
  par.par<-fitdist(empi,  "pareto", start=list(shape = 1, scale = 500))
  shape.par<-par.par$estimate[1]
  scale.par<-par.par$estimate[2]
  x.par<-rpareto(NROW(empi), shape.par,scale.par)

  # Theoretical Weibull random series
  par.wei<-fitdist(empi, "weibull")
  shape.wei<-par.wei$estimate[1]
  scale.wei<-par.wei$estimate[2]
  x.wei<-rweibull(NROW(empi), shape.wei,scale.wei)

  # Theoretical GEV random series
  # Fittig EVD using the "extRemes" package (can't get it with fitdist)
  par.gev <- fevd(empi,type =("GEV"),method=("Lmoments")) 
  loc.gev<-par.gev$results[1]
  shape.gev<-par.gev$results[3]
  scale.gev<-par.gev$results[2]
  x.gev<-rgev(NROW(empi), loc=loc.gev, scale=scale.gev, shape=shape.gev)


  # Create dataframe for using with ggplot+stat_ecdf
  df<-data.frame(cbind(empi,rep("Empirical",times=NROW(empi))))
  colnames(df)<-c("X","distr")
  dfpar<-data.frame(cbind(x.par,rep("Pareto",times=NROW(x.par))))
  colnames(dfpar)<-c("X","distr")
  dfwei<-data.frame(cbind(x.wei,rep("Weibull",times=NROW(x.wei))))
  colnames(dfwei)<-c("X","distr")
  dfgev<-data.frame(cbind(x.gev,rep("GEV",times=NROW(x.gev))))
  colnames(dfgev)<-c("X","distr")
  df<-rbind(df,dfpar)
  df<-rbind(df,dfwei)
  df<-rbind(df,dfgev)
  df$X<-as.numeric(levels(df$X))[df$X] 


  ggplot(df, aes(X, colour = distr, linetype = distr)) + stat_ecdf(size=1)+theme_classic() +
    scale_x_continuous(trans = 'log10')+scale_y_continuous(trans = 'log10') +
    xlab("Daily returns")+ylab("CDFs") + ggtitle(Name) + theme(plot.title = element_text(hjust = 0.5)) +
    theme(legend.position = c(0.85, 0.25), legend.text=element_text(size=8), legend.title=element_blank())

}

thecharts<-mapply(myFUN, df, names(df), SIMPLIFY = FALSE)

allthecharts<- marrangeGrob(thecharts, nrow=2, ncol=2)

ggsave("allthecharts.pdf", allthecharts)

以前,我错误地将命令g<-ggplot(df,...) print(g)添加到了mapply函数中,该函数在gList(var1 = list(data = list(list(colour = c(“#F8766D”,“#F8766D ”,:“ gList”中仅允许使用“ grobs”