I want to create a ggplot figure of autocorrelations for different subgroups of my data.
Using the forecast
package, I manage to produce a ggplot figure for the whole sample like this:
library(tidyverse)
library(forecast)
df <- data.frame(val = runif(100),
key = c(rep('a', 50), key = rep('b', 50)))
ggAcf(df$val)
Which produces:
But now I'm trying the following to produce the facets and it doesn't work:
ggplot(df) +
ggAcf(aes(val)) +
facet_wrap(~key)
Any ideas?
A possible solution building out the acf values and plot manually.
library(tidyverse)
library(forecast)
df <- data.frame(val = runif(100),
key = c(rep('a', 50), key = rep('b', 50)))
df_acf <- df %>%
group_by(key) %>%
summarise(list_acf=list(acf(val, plot=FALSE))) %>%
mutate(acf_vals=purrr::map(list_acf, ~as.numeric(.x$acf))) %>%
select(-list_acf) %>%
unnest() %>%
group_by(key) %>%
mutate(lag=row_number() - 1)
df_ci <- df %>%
group_by(key) %>%
summarise(ci = qnorm((1 + 0.95)/2)/sqrt(n()))
ggplot(df_acf, aes(x=lag, y=acf_vals)) +
geom_bar(stat="identity", width=.05) +
geom_hline(yintercept = 0) +
geom_hline(data = df_ci, aes(yintercept = -ci), color="blue", linetype="dotted") +
geom_hline(data = df_ci, aes(yintercept = ci), color="blue", linetype="dotted") +
labs(x="Lag", y="ACF") +
facet_wrap(~key)
library(forecast)
df <- data.frame(val = runif(100),
key = c(rep('a', 50), key = rep('b', 50)))
a = subset(df, key == "a")
ap = ggAcf(a$val)
b = subset(df, key == "b")
bp = ggAcf(b$val)
library(grid)
grid.newpage()
pushViewport(viewport(layout=grid.layout(1,2)))
print(ap, vp=viewport(layout.pos.row = 1, layout.pos.col = 1))
print(bp, vp=viewport(layout.pos.row = 1, layout.pos.col = 2))
Or:
grid.newpage()
pushViewport(viewport(layout=grid.layout(1,2)))
print(ap, vp=viewport(layout.pos.row = 1, layout.pos.col = 1))
print(bp, vp=viewport(layout.pos.row = 1, layout.pos.col = 2))
Adam Spannbauer's answer is excellent and the output is very similar to that of forecast::ggAcf
, with possibly just the dotted confidence limits lines differing from the dashed ones produced by ggAcf
(something very easy to fix if desired).
A quick and perhaps simpler alternative is to use ggfortify::autoplot
with a list for your different facet values as in the example below:
# Load ggfortify
require(ggfortify)
# Create sample data frame
df <- data.frame(val = runif(100),
key = c(rep('a', 50), key = rep('b', 50)))
# Create list with ACF objects for different key values
acf.key <- list()
for (i in 1:length(unique(df$key))) {
acf.key[[i]] <- acf(df$val[df$key==unique(df$key)[[i]]])
}
# Plot using ggfortify::autoplot
autoplot(acf.key, ncol=2)
Unfortunately it doesn't seem to be possible to get the banners with the facet titles above the plots as in standard ggplot
, so the final result is not as polished as that of the answer above. I was also unable to remove the y-axis label of the right-hand-side plot while keeping the label of the left-hand-side plot.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.