How to get ggplot2 to draw multiple simulated trajectories in same plot?

Hi! I want to draw multiple simulated paths on the same plot using ggplot2. But that I tried is not that I want :confused: Does anybody have an idea? I desire some graph like in the picture (not exactly with same labels).
Thanks in advance :smiley:

data <- tibble::tribble(
                 ~sim.1....sim.2....sim.3....sim.4....sim.5,
          "1  1085.000 1085.000 1085.000 1085.000 1085.000",
          "2  1085.344 1085.431 1086.190 1085.195 1085.730",
          "3  1085.485 1085.798 1086.167 1085.061 1085.864",
          "4  1085.555 1085.857 1088.121 1084.833 1085.805",
          "5  1085.349 1085.997 1087.619 1084.692 1086.209",
          "6  1085.341 1086.386 1087.414 1084.204 1085.859",
          "7  1085.312 1087.048 1087.727 1084.288 1085.003",
          "8  1085.119 1087.289 1088.487 1084.150 1085.170",
          "9  1085.503 1087.693 1088.319 1084.793 1084.747",
          "10 1085.993 1087.816 1088.512 1085.118 1084.765",
          "11 1086.189 1087.875 1088.465 1085.123 1083.272"
          )

data %>%
  gather(sim) %>%
  group_by(sim) %>%
  ggplot(aes(x=value,y=sim,color = sim)) + 
  geom_line() + 
  theme(legend.position = "none")

First you need to split your data into columns. Currently it is a single column.
Then you need to pivot_longer().
Then use ggplot.

library(tidyverse)

data <- tibble::tribble(
  ~sim.1....sim.2....sim.3....sim.4....sim.5,
  "1  1085.000 1085.000 1085.000 1085.000 1085.000",
  "2  1085.344 1085.431 1086.190 1085.195 1085.730",
  "3  1085.485 1085.798 1086.167 1085.061 1085.864",
  "4  1085.555 1085.857 1088.121 1084.833 1085.805",
  "5  1085.349 1085.997 1087.619 1084.692 1086.209",
  "6  1085.341 1086.386 1087.414 1084.204 1085.859",
  "7  1085.312 1087.048 1087.727 1084.288 1085.003",
  "8  1085.119 1087.289 1088.487 1084.150 1085.170",
  "9  1085.503 1087.693 1088.319 1084.793 1084.747",
  "10 1085.993 1087.816 1088.512 1085.118 1084.765",
  "11 1086.189 1087.875 1088.465 1085.123 1083.272"
)

data <- data %>%
  separate(col = 1, into = c("t", paste0("sim", 1:5)), sep = "\\s+") %>%
  mutate(across(everything(), as.numeric)) %>%
  pivot_longer(starts_with("sim"))

ggplot() +
  geom_line(data = data, mapping = aes(x = t, y = value, colour = name))


Created on 2021-03-02 by the reprex package (v1.0.0)

1 Like

Thank you! I really appreciate your help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.