plot three different time series of different lenghts in a single ggplot2 plot. in r

You can plot three different time series of different lengths in a single ggplot2 plot by first merging the three datasets into one using dplyr::full_join or merge. Then, you can melt the data into a long format using reshape2::melt or tidyr::gather functions. Finally, you can use ggplot2::geom_line to plot the time series.

Here is some example code:

main.r
library(dplyr)
library(tidyr)
library(ggplot2)

# create three different time series of different lengths
ts1 <- data.frame(time = seq(as.Date("2020-01-01"), by = "day", length.out = 30), value = rnorm(30))
ts2 <- data.frame(time = seq(as.Date("2020-02-01"), by = "day", length.out = 20), value = rnorm(20))
ts3 <- data.frame(time = seq(as.Date("2020-03-01"), by = "day", length.out = 40), value = rnorm(40))

# merge three datasets into one
df <- full_join(ts1, ts2, by = "time") %>% full_join(ts3, by = "time") 

# melt the data into a long format
df_long <- df %>% gather(key = "ts", value = "value", -time)

# plot the time series
ggplot(df_long, aes(x = time, y = value, color = ts)) +
  geom_line()
705 chars
19 lines

This will give you a plot with three different time series of different lengths in different colors.

gistlibby LogSnag