绘图时间多个来自不同tibles的序列

发布于 2025-01-21 13:13:07 字数 605 浏览 1 评论 0原文

我有下面的三个不同的tibble数据框架,我想将它们合并为一个图,有人可以帮助我吗?

”,ret_excess是最后一个”

如何将它们组合到一个图中?

我期待收到您的来信。

最好,迈克尔

I have three different tibble data fram as below and I want to combine them into one graph, is there anyone can help me?

this is one time series
this is the second one

and the ret_excess is the last one

how do I combine them into one plot?

I look forward to hear from you.

best, Michael

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2025-01-28 13:13:07

编辑:

请参阅Flick先生关于制定可再现例子的评论。另外,请参见Stackoverflow的问一个好问题。目前,尚不清楚您想做的事情是否对数据有任何意义。对于绘制多个时间序列的一般任务(在同一日期范围内),您可以参考以下答案。

在单独的数据帧(tibbles)中绘制多个时间序列,

您需要使用dplyr :: bind_cols函数将tibbles组合到较大的数据帧中。

然后,您可以使用ggplot为每个时间序列添加图层或行。

但是,我建议使用tidyr将数据转换为长格式,以获得最大的GGPLOT。

我做了一个基本 peprex 您的情况下。

library(tidyverse)

## Example data
a <- tibble(pred1 = c(.1, .01, .001, .0001))
b <- tibble(pred2 = c(.1, .2, .3, .4))
c <- tibble(irrelevant_col = c("dasda", "bvbcd", "cvb", "utyu"), 
            day = as.Date("2022-04-15") - 0:3,
            ret_excess = c(.1, -.1, .1, -.1))

## Merge
bind_data <- bind_cols(a, b, c)

## One Series at a time
ggplot(bind_data, aes(x = day)) + 
  geom_line(aes(y=pred1), col = "red", lty = "dashed", size = 1.5) +
  geom_line(aes(y=pred2), col = "blue", lty = "dotted", size = 1.5) +
  geom_line(aes(y=ret_excess), col = "green", size = 1.5)

## Using tidyr
bind_data %>%
  pivot_longer(c(pred1, pred2, ret_excess), names_to = "series") %>%
  ggplot(aes(x=day, col=series)) +
  geom_line(aes(y=value), size = 1.5)

img src =“ https://i.sstatic.net/kwyvn.png”

alt =“”> < >由

Edit:

See Mr. Flick's comment about formulating a reproducible example. Also, see stackoverflow's Asking a good question. Right now, it's not clear if what you want to do makes any sense for your data. For the general task of plotting multiple time series (over the same date range), you can refer to the answer below.

Plotting multiple time series in separate data frames (tibbles)

You'll want to combine the tibbles into a larger one using the dplyr::bind_cols function.

Then you can use ggplot to add layers, or lines, for each time series.

But I'd recommend transforming your data to a long format using tidyr to get the most out ggplot.

I made a basic reprex of your scenario below.

library(tidyverse)

## Example data
a <- tibble(pred1 = c(.1, .01, .001, .0001))
b <- tibble(pred2 = c(.1, .2, .3, .4))
c <- tibble(irrelevant_col = c("dasda", "bvbcd", "cvb", "utyu"), 
            day = as.Date("2022-04-15") - 0:3,
            ret_excess = c(.1, -.1, .1, -.1))

## Merge
bind_data <- bind_cols(a, b, c)

## One Series at a time
ggplot(bind_data, aes(x = day)) + 
  geom_line(aes(y=pred1), col = "red", lty = "dashed", size = 1.5) +
  geom_line(aes(y=pred2), col = "blue", lty = "dotted", size = 1.5) +
  geom_line(aes(y=ret_excess), col = "green", size = 1.5)

## Using tidyr
bind_data %>%
  pivot_longer(c(pred1, pred2, ret_excess), names_to = "series") %>%
  ggplot(aes(x=day, col=series)) +
  geom_line(aes(y=value), size = 1.5)

Created on 2022-04-15 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文