[英]plot time multiple series from different tibles
請參閱 Flick 先生關於制定可重現示例的評論。 另外,請參閱 stackoverflow 的問一個好問題。 目前,尚不清楚您想做什么對您的數據是否有意義。 對於繪制多個時間序列(在同一日期范圍內)的一般任務,您可以參考下面的答案。
您需要使用dplyr::bind_cols
function 將小標題組合成一個更大的小標題。
然后,您可以使用 ggplot 為每個時間序列添加層或線。
但我建議使用 tidyr 將您的數據轉換為長格式,以充分利用tidyr
。
我在下面對你的場景做了一個基本的代表。
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)
由reprex package (v2.0.1) 創建於 2022-04-15
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.