【问题标题】:grid.arrange + ggplot2 on Impulse Response Function (IRF)脉冲响应函数 (IRF) 上的 grid.arrange + ggplot2
【发布时间】:2017-08-11 01:09:03
【问题描述】:

我正在使用 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))

【问题讨论】:

    标签: r ggplot2 autoregressive-models


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 2015-09-09
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多