一个ggplot中的多个acf图。

时间:2018-05-24 01:23:06

标签: r ggplot2 time-series

我在R中使用以下代码绘制两个acf,如何在ggplot中制作相同的图,我尝试使用geom_area,但我无法获得相同的。

data1 <- seq(1, 3000, 3)
data2 <- seq(1, 1000, 0.5)
acf(data2, plot = T, lag.max = 300,col=2,ylim=c(0,1))
par(new=TRUE)
acf(data1, plot = T, lag.max = 300,col=1,ylim=c(0,1))

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法,

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与区域

ggplot(data=data[data$variable=="data2",], aes(x = lag, y = value, 
             fill = variable)) + geom_area(position = "dodge") +
  geom_area(data=data[data$variable=="data1",],
           aes(x = lag, y = value, fill=variable), position = "dodge")

带条的GGPLOT

ggplot(data=data[data$variable=="data2",], aes(x = lag, y = value, 
                                               fill = variable)) + geom_col(position = "dodge") +
  geom_col(data=data[data$variable=="data1",],
            aes(x = lag, y = value, fill=variable), position = "dodge")

您有以下两种方法的图表

enter image description here