How to use ggplot for plotting three series of different length

The following code

library(ggplot2)

p <- c(1.98139389065475e-08, 3.96278778130931e-08, 7.92557556261902e-08,
       1.58511511252372e-07, 3.17023022504761e-07, 6.3404604500949e-07,
       1.26809209001904e-06, 2.53618418003796e-06, 5.07236836007617e-06,
       1.01447367201518e-05, 2.02894734403047e-05, 4.05789468806074e-05,
       8.11578937612187e-05, 0.00450692446282339, 0.0331829332673235,
       0.0324267128112546, 0.536844723615457, 0.29007911330816,
       0.0821356435493368, 0.0206616530120613)

px <- c(0.0265956149070529, 0.100877042157315, 0.331154315180395,
        0.51467306280507, 0.0147007753919285, 0.0119991895582382)

q <- c(0.01765199, 0.06695391, 0.21979307, 0.34159776, 0.00975717,
       0.2448383, 0.0994078)

df <- data.frame(x = 1:length(p), p = p, px = px, q = q)

ggplot(df, aes(x = x)) +
  geom_line(aes(y = p, color = "p")) +
  geom_line(aes(y = px, color = "px")) +
  geom_line(aes(y = q, color = "q")) +
  labs(x = "Index", y = "Value") +
  scale_color_manual(values = c("p" = "red", "px" = "blue", "q" = "green")) +
  theme_minimal()

returns an error because, of course, the arguments of dataframe df imply a different number of rows: 20, 6, 7. How could I fix this problem? I would like to plot the series so that the p-series is drawn up to step 20, the q-series up to step 7 and the px-series up to step 6, i.e. up to their maximum length.

>Solution :

max_length <- max(sapply(list(p, px, q), length))
pad_vec <- function(x) { c(x, rep(NA, max_length - length(x)))}
df <- data.frame(x = 1:max_length, p = pad_vec(p), px = pad_vec(px), q = pad_vec(q))

But it would probably be more idiomatic for ggplot2 if you made one longer data frame stacking the series, with a column denoting which series each observation belongs to. Then you could use geom_line(aes(x = x, y = value, color = src)) in place of the three geom_line calls.

enter image description here

Leave a Reply