Error: Aesthetics must be either length 1 or the same as the data (121): yintercept

Error: Aesthetics must be either length 1 or the same as the data (121): yintercept

我有以下数据(从 1 到 1032),我正在尝试绘制自相关和部分自相关的相关图:

prp: 数据框

prp$Log.prp.Standardized:我要绘制的列

数据:

prp$Log.prp.Standardized(列名 - 我有 1 列有 1032 个值)

1       1.7923928339
2       0.7792383013
3      -0.2033400303
4      -1.7016479357
5       0.8002357419
6       0.3575677621
7       1.0209246410
8       0.7188631605
9      -0.5320108464
10     -0.2190886401
.
.
.
.
(till 1032)

我正在使用的功能:

correlogram <- function(x, type = "correlation"){
  gacf = acf(x, plot=FALSE, lag.max=120, type = type)
  gacf.df = with(gacf, data.frame(lag, acf))
  gacf.df$sig = qnorm((1 + 0.95)/2)/sqrt(length(x))
  q <- ggplot(data = gacf.df, mapping = aes(x = lag, y = acf))
  q <- q + xlim(c(0,120)) + theme_bw()
  q <- q + geom_hline(aes(yintercept = 0))
  q <- q + geom_segment(mapping = aes(xend = lag), yend = 0, lwd = 1)
  q <- q + geom_hline(aes(yintercept = c(sig, -1*sig)), linetype = 2, colour = "#e51843")
  if(type == "partial"){
    q <- q + ylab(expression(alpha[k]))
  } else {
    q <- q + ylab(expression(rho[k]))
  }
  q <- q + xlab("lag k")
}

那么我的代码是运行:

require(gridExtra)
library(gridExtra)
library(ggplot2)
library(grid)
q1 <- correlogram(prp$Log.prp.Standardized) + xlab(" ") + ggtitle("Total and Partial Correlograms")
q2 <- correlogram(prp$Log.prp.Standardized, type = "partial") 
grid.arrange (q1, q2, nrow = 2)
grid

但我收到以下错误:

Error: Aesthetics must be either length 1 or the same as the data (121): yintercept

任何帮助将不胜感激!

问题是您将 c(sig, -1*sig) 映射到 yintercept,这将不起作用,因为 c(sig, -1*sig) 的长度是 df gacf.df 长度的两倍。这就是错误消息告诉您的内容。有两种选择可以达到您想要的结果:

  1. 如果将 sig 添加为变量,则必须通过两次调用 geom_hline.

    添加水平线
  2. 下面的方法使 sig 成为一个标量。在那种情况下,您不必将 yintercept = c(sig, -1*sig) 包装在 aes() 内:

correlogram <- function(x, type = "correlation"){
  gacf = acf(x, plot=FALSE, lag.max=120, type = type)
  gacf.df = with(gacf, data.frame(lag, acf))
  #gacf.df$sig = qnorm((1 + 0.95)/2)/sqrt(length(x))
  
  sig = qnorm((1 + 0.95)/2)/sqrt(length(x))
  q <- ggplot(data = gacf.df, mapping = aes(x = lag, y = acf))
  q <- q + xlim(c(0,120)) + theme_bw()
  q <- q + geom_hline(aes(yintercept = 0))
  q <- q + geom_segment(mapping = aes(xend = lag), yend = 0, lwd = 1)
  # q <- q + geom_hline(aes(yintercept = sig), linetype = 2, colour = "#e51843")
  # q <- q + geom_hline(aes(yintercept = -1*sig), linetype = 2, colour = "#e51843")
  q <- q + geom_hline(yintercept = c(sig, -1*sig), linetype = 2, colour = "#e51843")
  if(type == "partial"){
    q <- q + ylab(expression(alpha[k]))
  } else {
    q <- q + ylab(expression(rho[k]))
  }
  q <- q + xlab("lag k")
}

library(gridExtra)
library(ggplot2)
library(grid)

set.seed(42)

prp <- data.frame(Log.prp.Standardized = rnorm(100))

q1 <- correlogram(prp$Log.prp.Standardized) + xlab(" ") + ggtitle("Total and Partial Correlograms")
q2 <- correlogram(prp$Log.prp.Standardized, type = "partial") 
grid.arrange (q1, q2, nrow = 2)

reprex package (v1.0.0)

于 2021-02-18 创建