-1

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

The Wolf
  • 11
  • 2
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please don't post data in images. How exactly do you want to represent these sets of numbers in the graph? – MrFlick Apr 15 '22 at 20:07
  • Hello. MrFlick. I want just a times series with those colums as input – The Wolf Apr 15 '22 at 20:13
  • The typical way would be to combine them into a data frame, pivot to longer if needed, and then plot where a "series" variable is mapped to color or group. It will be much easier to give specific help if you can give specific code examples of your data. The `dput()` function is often helpful for this, e.g. you might use `dput(head(FIRST_TABLE))` to produce code that will generate an exact replica of the first 6 rows of FIRST_TABLE. – Jon Spring Apr 15 '22 at 20:23
  • -lm_fit_pred is the data fram where .pred exists -nn_fit_pred is where data fram .pred exist for the second picture -data is data fram with ret_excess – The Wolf Apr 15 '22 at 20:29

1 Answers1

1

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)

aodwbag
  • 23
  • 5