impulse响应函数(IRF)上的grid.arrange + ggplot2

时间:2017-08-11 01:09:03

标签: r ggplot2 autoregressive-models

我正在使用GGplot2 + grid.arrange在Impulse-Response功能图(来自Vector AutoRegressive Model)中工作。下面我给你我的实际情节和vars包中的原始情节。我真的希望有任何提示来改善最终结果

会很好,至少将两个地块放得更近。

这不是一个完整的问题主题,而是一个改进的问题

这里是完整的代码

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)

实际输出 Actual Output

所需风格[致电plot(irf(my_var))Desired

1 个答案:

答案 0 :(得分: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