2

我正在使用 GGplot2 + grid.arrange 处理脉冲响应函数图(来自向量自回归模型)。下面我给你我的实际情节和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)

实际输出 实际输出

所需样式 [当您致电时plot(irf(my_var)) 期望的

4

1 回答 1

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)

在此处输入图像描述

于 2017-08-11T01:09:03.393 回答