I have three different tibble data fram as below and I want to combine them into one graph, is there anyone can help me?
how do I combine them into one plot?
I look forward to hear from you.
best, Michael
I have three different tibble data fram as below and I want to combine them into one graph, is there anyone can help me?
how do I combine them into one plot?
I look forward to hear from you.
best, Michael
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.
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)