简体   繁体   English

R中的多个ggAcf图

[英]Multiple ggAcf plot in R

I have 2 time series data and I want to plot the ACF in one ggplot with different colour, I just found ggAcf, but it couldn't solve my problem 我有2个时间序列数据,我想在一个具有不同颜色的ggplot中绘制ACF,我刚刚找到了ggAcf,但是它不能解决我的问题

library(ggplot2)
data1<-seq(1,300,3)
data2<-seq(1,100,0.5)
ggAcf(data2,1)
ggAcf(data2,20)

I want to make the plot as follow type 我想使情节如下

在此处输入图片说明

How can I solve it? 我该如何解决?

You can calculate the acf independently for each data set then add them to the same dataframe and plot from there 您可以为每个数据集独立计算acf ,然后将它们添加到相同的数据框中并从那里绘制

library(tidyr)
library(ggplot2)

data1 <- seq(1, 300, 3)
data2 <- seq(1, 100, 0.5)

acf1 <- acf(data1, plot = F, lag.max = 25)
acf2 <- acf(data2, plot = F, lag.max = 25)

acfs <- data.frame(lag = acf1$lag, acf1 = acf1$acf, acf2 = acf2$acf)

acfs %>% gather(key = data, value = acf, -lag) %>% ggplot(aes(x = lag, y = acf, 
  fill = data)) + geom_col(position = "dodge")

在此处输入图片说明

library(ggplot2)
library(reshape2)
data1 <- seq(1, 300, 3)
data2 <- seq(1, 100, 0.5)
acf1 <- acf(data1, plot = F, lag.max = 25)
acf2 <- acf(data2, plot = F, lag.max = 25)
df<- data.frame(lag = acf1$lag,acf1=acf1$acf,acf2=acf2$acf)
colnames(df)<-c("lag","data1","data2")
data<-melt(df,id="lag")
ggplot(data, aes(x=lag, y=value)) +
   geom_area(aes(colour = variable, fill= variable), position = 'stack') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM