【问题标题】:Error: Aesthetics must be either length 1 or the same as the data (121): yintercept错误:美学必须是长度 1 或与数据 (121) 相同:yintercept
【发布时间】:2021-02-18 05:21:59
【问题描述】:

我有以下数据(从 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

但我收到以下错误:

错误:美学长度必须为 1 或与数据 (121) 相同:yintercept

任何帮助将不胜感激!

【问题讨论】:

  • alpha[k]rho[k] 中。 alpharho 是什么?

标签: r ggplot2 autocorrelation


【解决方案1】:

问题是您将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 创建

【讨论】:

    猜你喜欢
    • 2016-10-13
    • 1970-01-01
    • 2017-11-04
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    相关资源
    最近更新 更多