Dev P
Dev P

Reputation: 449

Is there a way to plot 2 or more acfs under single plot

I have a time series data frame. I have two continuous variables for which I need to plot the autocorrelation (acf) functions. I am trying to use plot_grid() to make sure that two plots are shown in a single window, but that is not happening. Here is the example:

#df is dataframe; col1 and col2 are continuous variables

p1 <- acf(df$col1,lag.max = 5 ,plot = TRUE) 
p2 <- acf(df$col2,lag.max = 5 ,plot = TRUE)
plot_grid(p1, p2, nrow = 2,ncol =1,rel_heights = c(2/1,2/1),rel_widths = c(2/2,2/2))
    f <- structure(list(Date = structure(c(1505779200, 1505779500, 1505779800, 
 1505780100), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
     A = c(212.429693925327, 211.464088210329, 211.653306685973, 
     210.981936189015), B= c(75.9448191760481, 76.2501222022257, 
     76.1316674891558, 76.8299563088116)), row.names = c(NA, 4L
 ), class = "data.frame")
p1 <- acf(f$A, lag.max = 5, plot = FALSE) 
p2 <- acf(f$B, lag.max = 5, plot = FALSE)

cowplot::plot_grid(autoplot(p1), autoplot(p2), nrow = 2)

Upvotes: 0

Views: 709

Answers (3)

UseR10085
UseR10085

Reputation: 8166

If you are using base R, then the following code can be used

par(mfrow=c(1,2))
for(i in 2:ncol(f)) {
  y <- f[,i]
  name <- names(f)[i]
  plot(acf(y,plot=F, na.action = na.pass)[1:5],main=name)
}

enter image description here

Upvotes: 0

kath
kath

Reputation: 7724

A grid-based solution could be

library(ggfortify)

p1 <- acf(my_df$A, lag.max = 5, plot = FALSE)
p2 <- acf(my_df$B, lag.max = 5, plot = FALSE)

cowplot::plot_grid(autoplot(p1), autoplot(p2), nrow = 2)

enter image description here

If you want to add titles or labels to the plot you can do:

cowplot::plot_grid(autoplot(p1) + ggtitle("A"), autoplot(p2) + ggtitle("B"), nrow = 2)

# "AUTO" adds captial letters A, B, ... to the plots, but 
# you can also specify your own labels as a vector
cowplot::plot_grid(autoplot(p1), autoplot(p2), nrow = 2, labels = "AUTO") 

Data

my_df <-
  structure(list(Date = structure(c(1505779200, 1505779500, 1505779800, 1505780100),
                                  class = c("POSIXct", "POSIXt"), tzone = "UTC"),
                 A = c(212.429693925327, 211.464088210329, 211.653306685973, 210.981936189015),
                 B = c(75.9448191760481, 76.2501222022257, 76.1316674891558, 76.8299563088116)),
            row.names = c(NA, 4L),
            class = "data.frame")

Upvotes: 0

Ben Bolker
Ben Bolker

Reputation: 226182

plot_grid is from the cowplot package (I think) and works on grid-based graphics objects (i.e. output from ggplot2/lattice). acf() uses base graphics. Something using par(mfrow ...) should work, like this:

orig_pars <- par(mfrow=c(1,2))
acf(df$col1,lag.max = 5 ,plot = TRUE) 
acf(df$col2,lag.max = 5 ,plot = TRUE)
pars(orig_pars) ## reset

Or you can

  • look around for a grid-based ACF plot solution: someone may have written ggacf or the equivalent
  • extract the data from the acf() object and create your own plots with lattice/ggplot

Upvotes: 1

Related Questions