代码之家  ›  专栏  ›  技术社区  ›  Xia.Song

一个GG图中有多个ACF图。

  •  1
  • Xia.Song  · 技术社区  · 6 年前

    我用下面的代码在R中绘制了两个acf,如何在ggplot中生成相同的图,我尝试使用geom ou区域,但我不能得到相同的图。

    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 回复  |  直到 6 年前
        1
  •  1
  •   PKumar    6 年前

    你可以尝试下面的方法,

    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")
    

    G面积图

    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(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 :