rggplot2autoregressive-models

grid.arrange + ggplot2 on Impulse Response Function (IRF)


I'm working in a Impulse-Response function plot (from a Vector AutoRegressive Model) with GGplot2 + grid.arrange. Below i give you my actual plot and the original one from the vars package. I really would like any hint to improve the final result

Would be nice, at least place both plots closer.

This is not a full question topic, but an improvement asking

here the full code

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 Actual Output

Desired Style [when you call plot(irf(my_var)) Desired


Solution

  • Got something very close to desired model.

    here the changed 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(),
                      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