繁体   English   中英

plot 中的当前相关性在多线时间序列的两个时间序列之间

[英]Present correlation in plot between two time series for a multiline time series

我有许多图形,上面绘制了两个时间序列。

也就是说,我有一个 y_1 和 y_2 的 plot 针对一组共同的日期。

对于每个 plot,我想介绍每对系列之间 plot 的相关性。 也就是说,我想计算:cor(y_1,y_2) 并将结果数字包含在每个 plot 上。

这在 ggplot2 中以一种有原则的方式很难做到。 到目前为止,我还没有找到使用 stat_cor 的简单方法。

我已经查看了为此任务推荐的其他函数,但它们都是为报告 y_1 和 y_2 的相关性而设计的,其中 y_1 是 plot 与 y_2 而不是 y_1 和 y_2 都是 plot 与时间。

我更喜欢 ggplot2-ish 的方式来做到这一点,但我愿意在 R 中使用任何图形软件。 这是一个最小工作示例的代码以及我尝试过的代码。

library(reprex); library(ggplot2); library(ggpubr)
n <- 6; 
Q=sample(18:30, n, replace=TRUE)

# make sample data
dat <- data.frame(id=1:n, 
                  date=seq.Date(as.Date("2020-12-26"), as.Date("2020-12-31"), "day"),
                  group=rep(LETTERS[1:2], n/2),
                  quantity= Q,
                  price= 100 - 2*Q + rnorm(n))
dat
#>   id       date group quantity    price
#> 1  1 2020-12-26     A       19 63.02628
#> 2  2 2020-12-27     B       26 49.66597
#> 3  3 2020-12-28     A       27 44.98031
#> 4  4 2020-12-29     B       24 51.11224
#> 5  5 2020-12-30     A       29 41.11129
#> 6  6 2020-12-31     B       28 43.04494

tseriesplot <- ggplot(dat, aes(x = date)) + ggtitle("Oil: Daily Quantity and Price") +
                  geom_line(aes(y = Q, color = "Quantity (thousands of barrels)")) +
                  geom_line(aes(y = price, color = "Price"))
  
tseriesplot


# naive attempt fails
tseriesplot + stat_cor(data = dat, aes(x=quantity, y=price),method="pearson")
#> Error: Invalid input: date_trans works with objects of class Date only

代表 package (v0.3.0) 于 2021 年 1 月 5 日创建

我认为这将是一个好问题,因为它类似于其他地方更复杂的问题,例如https://stat.ethz.ch/pipermail/r-help/2020-July/467805.html但更基本。

1) annotate创建你想要的文本txt plot 然后使用annotate

txt <- with(dat, sprintf("cor: %.2f", cor(quantity, price)))
tseriesplot + 
  annotate("text", label = txt, x = min(dat$date), y = max(dat$quantity, dat$price), 
    hjust = -0.1)

截屏

2) grid.text另一种方法是使用网格图形,它允许独立于数据指定位置。 使用上面的txt

library(grid)

tseriesplot
grid.text(txt, 0.1, 0.9)

3a)动物园这也可以:

library(zoo)

z <- read.zoo(dat[c("date", "price", "quantity")])
txt <- sprintf("cor: %.2f", cor(z)[2])
autoplot(z, facet = NULL) +
  annotate("text", label = txt, x = start(z), y = max(z), hjust = -0.1)

3b) 规模

或者您可以缩放变量,因为这不会影响相关性:

z <- scale(z)
autoplot(z, facet = NULL) +
  annotate("text", label = txt, x = start(z), y = max(z), hjust = -0.1)

讨论

总体上将不同解决方案的部分组合在一起,这似乎是最紧凑的

library(zoo)
library(grid)

z <- read.zoo(dat[c("date", "price", "quantity")])
autoplot(z, facet = NULL)
grid.text(sprintf("cor: %.2f", cor(z)[2]), 0.1, 0.9)

您可以简单地计算相关系数并将其作为注释添加到 plot 中,而不是试图弄清楚如何使用ggpubr::stat_cor执行此操作,例如使用annotate

library(ggplot2)
library(ggpubr)

set.seed(42)

n <- 6; 
Q=sample(18:30, n, replace=TRUE)

# make sample data
dat <- data.frame(id=1:n, 
                  date=seq.Date(as.Date("2020-12-26"), as.Date("2020-12-31"), "day"),
                  group=rep(LETTERS[1:2], n/2),
                  quantity= Q,
                  price= 100 - 2*Q + rnorm(n))
dat
#>   id       date group quantity    price
#> 1  1 2020-12-26     A       18 64.63286
#> 2  2 2020-12-27     B       22 56.40427
#> 3  3 2020-12-28     A       18 63.89388
#> 4  4 2020-12-29     B       26 49.51152
#> 5  5 2020-12-30     A       27 45.90534
#> 6  6 2020-12-31     B       21 60.01842

tseriesplot <- ggplot(dat, aes(x = date)) + ggtitle("Oil: Daily Quantity and Price") +
  geom_line(aes(y = quantity, color = "Quantity (thousands of barrels)")) +
  geom_line(aes(y = price, color = "Price"))

tseriesplot +
  annotate("text", 
           x = min(dat$date), 
           y = 70, 
           label = paste0("p = ", scales::number(cor(dat$quantity, dat$price, method = "pearson"), accuracy = .01)),
           hjust = 0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM