2017-08-11 11 views
1

私はGGplot2 + grid.arrangeを使ったImpulse-Response関数プロット(Vector AutoRegressive Modelから)で作業しています。下に私はあなたに私の実際のプロットとvarsパッケージから元のものを与える。インパルス応答関数(IRF)のgrid.arrange + ggplot2

少なくとも両方のプロットを近くに配置するとよいでしょう。

あなたはplot(irf(my_var)) Desired

呼び出すときに、実際の出力 Actual Output

は、[スタイルを希望

library(vars) 

# Define lags 
lag = VARselect(my_data, lag.max=12) 

# Estimating var 
my_var = VAR(my_data, min(lag$selection), type='both') 

# Set the Impulse-Response data 
impulse <- irf(my_var) 

# Prepare plot data 
    number_ticks <- function(n) {function(limits) pretty(limits, n)} 
    lags <- c(1:11) 

    irf1<-data.frame(impulse$irf$PIB[,1],impulse$Lower$PIB[,1], 
        impulse$Upper$PIB[,1], lags) 
    irf2<-data.frame(impulse$irf$PIB[,2],impulse$Lower$PIB[,2], 
        impulse$Upper$PIB[,2]) 

# creating plots 

PIB_PIB <- ggplot(data = irf1,aes(lags,impulse.irf.PIB...1.)) + 
      geom_line(aes(y = impulse.Upper.PIB...1.), colour = 'lightblue2') + 
      geom_line(aes(y = impulse.Lower.PIB...1.), colour = 'lightblue')+ 
      geom_line(aes(y = impulse.irf.PIB...1.))+ 
      geom_ribbon(aes(x=lags, ymax=impulse.Upper.PIB...1., ymin=impulse.Lower.PIB...1.), fill="lightblue", alpha=.1) + 
      xlab("") + ylab("PIB") + ggtitle("Orthogonal Impulse Response from PIB") + 
      theme(axis.title.x=element_blank(), 
        axis.text.x=element_blank(),      
        axis.ticks.x=element_blank()) + 
      geom_line(colour = 'black') 



PIB_CON <- ggplot(data = irf2,aes(lags,impulse.irf.PIB...2.)) + 
      geom_line(aes(y = impulse.Upper.PIB...2.), colour = 'lightblue2') + 
      geom_line(aes(y = impulse.Lower.PIB...2.), colour = 'lightblue')+ 
      geom_line(aes(y = impulse.irf.PIB...2.))+ 
      geom_ribbon(aes(x=lags, ymax=impulse.Upper.PIB...2., ymin=impulse.Lower.PIB...2.), fill="lightblue", alpha=.1) + 
      scale_x_continuous(breaks=number_ticks(10)) + 
      xlab("") + ylab("CONSUMO") + ggtitle("") + 
      theme(axis.title.x=element_blank(), 
        axis.text.x=element_blank(),      
        axis.ticks.x=element_blank()) + 
      geom_line(colour = 'black') 


# Generating plot 

grid.arrange(PIB_PIB, PIB_CON, nrow=2) 

ここに完全なコードこれは、完全な質問トピックではなく、改善が

を求めて

答えて

1

非常に近いものを得ましたモデル。ここ

変更プロット:

PIB_PIB <- ggplot(data = irf1,aes(lags,impulse.irf.PIB...1.)) + 
      geom_line(aes(y = impulse.Upper.PIB...1.), colour = 'lightblue2') + 
      geom_line(aes(y = impulse.Lower.PIB...1.), colour = 'lightblue')+ 
      geom_line(aes(y = impulse.irf.PIB...1.))+ 
      geom_ribbon(aes(x=lags, ymax=impulse.Upper.PIB...1., ymin=impulse.Lower.PIB...1.), fill="lightblue", alpha=.1) + 
      xlab("") + ylab("PIB") + ggtitle("Orthogonal Impulse Response from PIB") + 
      theme(axis.title.x=element_blank(), 
        axis.text.x=element_blank(),      
        axis.ticks.x=element_blank(), 
        plot.margin = unit(c(2,10,2,10), "mm"))+ 
      scale_x_continuous(breaks=number_ticks(10)) + 
      geom_line(colour = 'black') 



PIB_CON <- ggplot(data = irf2,aes(lags,impulse.irf.PIB...2.)) + 
      geom_line(aes(y = impulse.Upper.PIB...2.), colour = 'lightblue2') + 
      geom_line(aes(y = impulse.Lower.PIB...2.), colour = 'lightblue')+ 
      geom_line(aes(y = impulse.irf.PIB...2.))+ 
      geom_ribbon(aes(x=lags, ymax=impulse.Upper.PIB...2., ymin=impulse.Lower.PIB...2.), fill="lightblue", alpha=.1) + 
      xlab("") + ylab("CONSUMO") + ggtitle("") + 
      theme(axis.title.x=element_blank(), 
     #   axis.text.x=element_blank(),      
     #   axis.ticks.x=element_blank(), 
        plot.margin = unit(c(-10,10,4,10), "mm"))+ 
      scale_x_continuous(breaks=number_ticks(10)) + 
      geom_line(colour = 'black') 

grid.arrange(PIB_PIB, PIB_CON, nrow=2) 

enter image description here

関連する問題