ggplot的互相关函数(CCF)的问题

时间:2019-04-15 00:59:30

标签: r ggplot2 correlation cross-correlation

我正在使用互相关函数(ccf)来显示来自不同弹簧的流量的日延迟。我使用ccf to ggplot code以获得更好的可视化效果。我的结果看起来像下面的图片,代码可以正常工作,但是后来我重新运行代码,现在我一直遇到错误。我最初对源代码进行了一些修改,但是一旦出现错误,我就返回原始源代码,并且错误仍然存​​在!

数据,代码和问题:

# example data   
big = runif(60)
mangum = runif(61:120)
hydro = data.frame(big, mangum)

# Using the ccf to ggplot function (from the above link)
xcf_plot <- function(df, x, y, title = "Cross Correlation"){
  df_x <- eval(substitute(x), df)
  df_y <- eval(substitute(y), df)
  ccf.object <- ccf(df_x, df_y, plot = FALSE)
  output_table <- cbind(lag = ccf.object$lag, 
                        x.corr = ccf.object$acf) %>%
                          as_tibble() %>%
                          mutate(cat = ifelse(x.corr > 0, "green", "red"))
  output_table %>%
    ggplot(aes(x = lag, y = x.corr)) +
    geom_bar(stat = "identity", aes(fill = cat))+
    scale_fill_manual(values = c("#339933", "#cc0000"))+
    ylab("Cross correlation")+
    scale_y_continuous(limits = c(-1, 1))+
    theme_bw()+ 
    theme(legend.position = "none", 
          plot.title = element_text(size=10))+
    ggtitle(title) -> p

  ggsave(paste(title, ".jpg"), plot = p, height = 2.7, width = 4, units = "in")
}     
# The only thing I originally changed from the source code was ".svg" to ".jpg", 
# "theme_economist()" to "theme_bw()" and colors.

# run the function
xcf_plot(df = hydro, x = hydro$big, y = hydro$mangum, title = "big and mangum")

错误消息:

  

ifelse(x.corr> 0,“绿色”,“红色”)中的错误:对象'x.corr'不是   找到

知道为什么会这样吗?这特别奇怪,因为我以前可以使用完全相同的代码,并且可以完美地工作:

flawlessly

这是我的会话信息:

R version 3.4.3 (2017-11-30),
Platform: x86_64-w64-mingw32/x64 (64-bit),
Running under: Windows 10 x64 (build 16299)

0 个答案:

没有答案