Search code examples
rgridforecast

Making facet with gglagplot


I am using package forecast() and I trying to making facet with gglagplot function.

#Code
library(forecast)
library(gridExtra)


# GGLAGPLOT 1
 gg1<-gglagplot(TEST_1,lags = 52)

# GGLAGPLOT 2       
 gg2<-gglagplot(TEST_2,lags = 52)

# GGLAGPLOT 3
 gg3<-gglagplot(TEST_3,lags = 52)

And this pic below is example of one of three gglagplot.

enter image description here

I try with this function below but my facet is not good and look like pic below.

 # Trying facet
grid.arrange(gg1,gg2,gg3)

So can anybody help me to make facet like facet below ? enter image description here

So can anybody help me to make better facet like pic below ?

enter image description here


Solution

  • If all three plots have the same legend, you can remove the legends from the plots and save it separately using the package ggpubr:

    #Code
    library(forecast)
    library(gridExtra)
    library(ggpubr)
    
    # GGLAGPLOT 1
     gg1<-gglagplot(TEST_1,lags = 52) + theme(legend.position = "none")
    
    # GGLAGPLOT 2       
     gg2<-gglagplot(TEST_2,lags = 52) + theme(legend.position = "none")
    
    # GGLAGPLOT 3
     gg3<-gglagplot(TEST_3,lags = 52)
    
    # SAVE LEGEND
    gg3_legend = get_legend(gg3)
    
    
    # Plot everything together
    grid.arrange(gg1,gg2,gg3 + theme(legend.position = "none"), as_ggplot(gg3_legend), nrow = 1)
    

    If you don't save the legend separately, the last plot would be smaller than the first two.