I use ggplot to plot hundreds of simulated paths. The data has been organized by pivot_longer to look like this (200 simulated paths, each having 2520 periods; simulation 1 first, then simulation 2 etc., with ind showing the simulated values for each period):
| sim | period | ind |
|---|---|---|
| 1 | 0 | 100.0 |
| 1 | 1 | 99.66 |
| . | . | . |
| 1 | 2520 | 103.11 |
| 2 | 0 | 100.0 |
| . | . | . |
| . | . | . |
| 200 | 0 | 100.0 |
| . | . | . |
| 200 | 2520 | 195.11 |
Not sure if using pivot_long is optimal or not but at least the following ggplot looks fine:
p<-ggplot(simdata, aes(x=period, y=ind,color=sim, group=sim))+geom_line()
producing a nice graph with paths in different shades of blue.
What I would like to do is to color the mean, median and quartile paths with different colours (e.g. red and green). Median, mean and quartile paths are defined by the last period's value. I already know the sim number for those. E.g. let's assume that median path is the one where sim = 160.
I have tried the following approaches.
Add a new geom_line specifying the number (sim) of the median path:
p + geom_line(aes(y = simdata[sim == 160,], color ="red")
This fails since the additional geom_line is not of the same length (200*2520) as the simdata - even if the graph's x-axis only has 2520 periods.
Stat_summary
p + stat_summary(aes(group=sim),fun=median, geom="line",colour="red")
The outcome was that all lines become read, also the simulated ones. Also, I rejected this since it takes a lot more time to have ggplot to find the mean, median etc. values rather than finding them before the graphics part.
- gghighlight
I experimented with this package but could not figure out if you can specify the path numbers to color.

