Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add three lines at the end of one time series in ggplot?

I have the following dataframe:


df = structure(list(Date = structure(c(18421, 18422, 18423, 18424, 
18425, 18428, 18429, 18430, 18431, 18432, 18435, 18436, 18437, 
18438, 18439, 18442, 18443, 18444, 18445, 18446, 18449, 18450, 
18451, 18452, 18453, 18456, 18457, 18458, 18459, 18460, 18463, 
18464, 18465, 18466, 18467, 18470, 18471, 18472, 18473, 18474, 
18477, 18478, 18479, 18480, 18481, 18484, 18485, 18486, 18487, 
18488, 18491, 18492, 18493, 18494, 18495, 18498, 18499, 18500, 
18501, 18502, 18505, 18506, 18507, 18508, 18509, 18512, 18513, 
18514, 18515, 18516, 18519, 18520, 18521, 18522, 18523, 18526, 
18527, 18528, 18529, 18530, 18533, 18534, 18535, 18536, 18537, 
18540, 18541, 18542, 18543, 18544, 18547, 18548, 18549, 18550, 
18551, 18554, 18555, 18556, 18557, 18558, 18561, 18562, 18563, 
18564, 18565, 18568, 18569, 18570, 18571, 18572, 18575, 18576, 
18577, 18578, 18579, 18582, 18583, 18584, 18585, 18586, 18589, 
18590, 18591, 18592, 18593, 18596, 18597, 18598, 18599, 18600, 
18603, 18604, 18605, 18606, 18607, 18610, 18611, 18612, 18613, 
18614, 18617, 18618, 18619, 18620, 18621, 18624, 18625, 18626, 
18627, 18628, 18631, 18632, 18633, 18634, 18635, 18638, 18639, 
18640, 18641, 18642, 18645, 18646, 18647, 18648, 18649, 18652, 
18653, 18654, 18655, 18656, 18659, 18660, 18661, 18662, 18663, 
18666, 18667, 18668, 18669, 18670, 18673, 18674, 18675, 18676, 
18677, 18680, 18681, 18682, 18683, 18684, 18687, 18688, 18689, 
18690, 18691, 18694, 18695, 18696, 18697, 18698, 18701, 18702, 
18703, 18704, 18705, 18708, 18709, 18710, 18711, 18712, 18715, 
18716, 18717, 18718, 18719, 18722, 18723, 18724, 18725, 18726, 
18729, 18730, 18731, 18732, 18733, 18736, 18737, 18738, 18739, 
18740, 18743, 18744, 18745, 18746, 18747, 18750, 18751, 18752, 
18753, 18754, 18757, 18758, 18759, 18760, 18761, 18764, 18765, 
18766, 18767, 18768, 18771, 18772, 18773, 18774, 18775, 18778, 
18779, 18780, 18781, 18782, 18785, 18786, 18787, 18788, 18789, 
18792, 18793, 18794, 18795, 18796, 18799, 18800, 18801, 18802, 
18803, 18806, 18807, 18808, 18809, 18810, 18813, 18814, 18815, 
18816, 18817, 18820, 18821, 18822, 18823, 18824, 18827, 18828, 
18829, 18830, 18831, 18834, 18835, 18836, 18837, 18838, 18841, 
18842, 18843, 18844, 18845, 18848, 18849, 18850, 18851, 18852, 
18855, 18856, 18857, 18858, 18859, 18862, 18863, 18864, 18865, 
18866, 18869, 18870, 18871, 18872, 18873, 18876, 18877, 18878, 
18879, 18880, 18883, 18884, 18885, 18886, 18887, 18890, 18891, 
18892, 18893, 18894, 18897, 18898, 18899, 18900, 18901, 18904, 
18905, 18906, 18907, 18908, 18911, 18912, 18913, 18914, 18915, 
18918, 18919, 18920, 18921, 18922, 18925, 18926, 18927, 18928, 
18929, 18932, 18933, 18934, 18935, 18936, 18939, 18940, 18941, 
18942, 18943, 18946, 18947, 18948, 18949, 18950, 18953, 18954, 
18955, 18956, 18957, 18960, 18976, 18976, 18976), class = "Date"), 
    Data = c(8.9, 8.7, 8.68, 8.355, 8.19, 8.2, 8.5, 8.4, 8.26, 
    7.55, 7.54, 7.9, 7.66, 7.6, 7.5, 7.6, 7.95, 8, 8, 8, 7.96, 
    7.91, 8.03, 8.46, 9.113, 9.2, 9.37, 9.31, 9.06, 9, 8.95, 
    8.88, 8.91, 8.85, 8.77, 8.62, 8.6, 8.56, 8.52, 8.52, 8.38, 
    8.51, 8.49, 8.6, 8.62, 8.5, 8.45, 8.6, 8.84, 8.87, 9.06, 
    9.23, 9.15, 9.15, 9.2, 9.15, 9.18, 9.15, 9.1, 9.3, 9.27, 
    9.3, 9.22, 9.1, 9.19, 9.19, 9, 8.77, 8.7, 8.63, 8.49, 8.26, 
    8.2, 7.8, 7.87, 8.1, 8.3, 8.12, 8.2, 8.52, 8.65, 8.78, 8.88, 
    8.91, 8.85, 8.9, 8.82, 8.82, 8.84, 8.8, 8.75, 8.85, 8.89, 
    8.99, 9, 8.97, 9.15, 9.01, 9.01, 9.03, 8.9, 8.76, 8.593, 
    8.6, 8.2, 8, 8.555, 8.5, 8.73, 9, 9, 8.93, 8.99, 8.96, 8.9, 
    8.9, 8.89, 9.3, 11.25, 10.74, 10.55, 10.7, 10.9, 10.9, 10.857, 
    10.64, 10.51, 10.52, 10.57, 10.69, 10.67, 10.16, 10.65, 10.85, 
    10.59, 10.25, 8.795, 8.83, 8.75, 9.51, 9.85, 10.05, 9.67, 
    9.02, 9.02, 8.6, 8.78, 8.79, 8.58, 8.58, 8.4, 7.86, 7.47, 
    6.8, 6.65, 6.75, 6.8, 6.13, 6.47, 5.8, 5.8, 5.89, 6.29, 6.01, 
    5.95, 5.92, 6.01, 5.93, 5.66, 5.75, 5.52, 5.77, 5.74, 5.55, 
    5.54, 5.49, 5.59, 5.65, 5.7, 6.1, 6.1, 6.1, 6.08, 5.92, 5.76, 
    5.9, 5.75, 5.7, 5.52, 5.67, 5.88, 5.8, 5.75, 5.8, 5.73, 5.68, 
    5.75, 5.71, 5.69, 5.67, 5.72, 5.69, 5.68, 5.55, 5.53, 5.52, 
    5.5, 5.47, 5.42, 5.3, 5.34, 5.41, 5.55, 5.65, 5.65, 5.85, 
    6.04, 6.32, 6.85, 6.75, 6.613, 6.59, 6.55, 6.56, 6.76, 6.66, 
    6.6, 6.72, 6.68, 6.62, 6.67, 6.597, 6.5, 6.38, 6.28, 6.2, 
    6, 6.12, 6.24, 6.25, 6.31, 6.26, 6.3, 6.34, 6.45, 6.31, 6.19, 
    6.05, 6.2, 6.23, 6.1, 6.06, 6.03, 6.32, 6.37, 6.37, 6.49, 
    6.6, 6.36, 6.45, 6.64, 6.6, 6.55, 6.6, 6.73, 6.78, 6.69, 
    6.75, 6.75, 6.69, 6.67, 6.7, 2.52, 2.14, 2.12, 2.31, 2.25, 
    2.13, 2.16, 2.12, 2.12, 2, 1.97, 1.92, 1.9, 1.85, 1.85, 1.85, 
    1.86, 1.78, 1.68, 1.709, 2.04, 2.11, 2.05, 2.04, 1.99, 2.03, 
    2, 1.95, 1.95, 1.92, 1.84, 1.89, 1.86, 1.74, 1.76, 1.75, 
    1.71, 1.7, 1.71, 1.6, 1.76, 1.685, 1.63, 1.61, 1.67, 1.75, 
    1.81, 1.81, 1.79, 1.76, 1.79, 1.76, 1.76, 1.76, 1.76, 1.7, 
    1.68, 1.68, 1.93, 2.37, 2.55, 2.33, 2.19, 2.03, 2.17, 2.31, 
    2.46, 2.45, 2.47, 2.46, 2.53, 2.6, 2.57, 2.47, 2.42, 2.2, 
    2.24, 2.24, 2.289, 2.32, 2.32, 2.36, 2.34, 2.38, 2.66, 2.71, 
    2.74, 2.89, 2.85, 2.96, 3.27, 3.36, 3.48, 3.45, 3.53, 3.525, 
    3.51, 3.5, 3.496, 3.47, 3.47, 3.42, 3.46, 3.47, 3.45, 3.46, 
    3.47, 3.46, 3.4, 3.44, 3.43, 3.43, 3.4, 3.33, 3.33, 4.01, 
    2.5)), row.names = c(2208L, 2209L, 2210L, 2211L, 2212L, 2213L, 
2214L, 2215L, 2216L, 2217L, 2218L, 2219L, 2220L, 2221L, 2222L, 
2223L, 2224L, 2225L, 2226L, 2227L, 2228L, 2229L, 2230L, 2231L, 
2232L, 2233L, 2234L, 2235L, 2236L, 2237L, 2238L, 2239L, 2240L, 
2241L, 2242L, 2243L, 2244L, 2245L, 2246L, 2247L, 2248L, 2249L, 
2250L, 2251L, 2252L, 2253L, 2254L, 2255L, 2256L, 2257L, 2258L, 
2259L, 2260L, 2261L, 2262L, 2263L, 2264L, 2265L, 2266L, 2267L, 
2268L, 2269L, 2270L, 2271L, 2272L, 2273L, 2274L, 2275L, 2276L, 
2277L, 2278L, 2279L, 2280L, 2281L, 2282L, 2283L, 2284L, 2285L, 
2286L, 2287L, 2288L, 2289L, 2290L, 2291L, 2292L, 2293L, 2294L, 
2295L, 2296L, 2297L, 2298L, 2299L, 2300L, 2301L, 2302L, 2303L, 
2304L, 2305L, 2306L, 2307L, 2308L, 2309L, 2310L, 2311L, 2312L, 
2313L, 2314L, 2315L, 2316L, 2317L, 2318L, 2319L, 2320L, 2321L, 
2322L, 2323L, 2324L, 2325L, 2326L, 2327L, 2328L, 2329L, 2330L, 
2331L, 2332L, 2333L, 2334L, 2335L, 2336L, 2337L, 2338L, 2339L, 
2340L, 2341L, 2342L, 2343L, 2344L, 2345L, 2346L, 2347L, 2348L, 
2349L, 2350L, 2351L, 2352L, 2353L, 2354L, 2355L, 2356L, 2357L, 
2358L, 2359L, 2360L, 2361L, 2362L, 2363L, 2364L, 2365L, 2366L, 
2367L, 2368L, 2369L, 2370L, 2371L, 2372L, 2373L, 2374L, 2375L, 
2376L, 2377L, 2378L, 2379L, 2380L, 2381L, 2382L, 2383L, 2384L, 
2385L, 2386L, 2387L, 2388L, 2389L, 2390L, 2391L, 2392L, 2393L, 
2394L, 2395L, 2396L, 2397L, 2398L, 2399L, 2400L, 2401L, 2402L, 
2403L, 2404L, 2405L, 2406L, 2407L, 2408L, 2409L, 2410L, 2411L, 
2412L, 2413L, 2414L, 2415L, 2416L, 2417L, 2418L, 2419L, 2420L, 
2421L, 2422L, 2423L, 2424L, 2425L, 2426L, 2427L, 2428L, 2429L, 
2430L, 2431L, 2432L, 2433L, 2434L, 2435L, 2436L, 2437L, 2438L, 
2439L, 2440L, 2441L, 2442L, 2443L, 2444L, 2445L, 2446L, 2447L, 
2448L, 2449L, 2450L, 2451L, 2452L, 2453L, 2454L, 2455L, 2456L, 
2457L, 2458L, 2459L, 2460L, 2461L, 2462L, 2463L, 2464L, 2465L, 
2466L, 2467L, 2468L, 2469L, 2470L, 2471L, 2472L, 2473L, 2474L, 
2475L, 2476L, 2477L, 2478L, 2479L, 2480L, 2481L, 2482L, 2483L, 
2484L, 2485L, 2486L, 2487L, 2488L, 2489L, 2490L, 2491L, 2492L, 
2493L, 2494L, 2495L, 2496L, 2497L, 2498L, 2499L, 2500L, 2501L, 
2502L, 2503L, 2504L, 2505L, 2506L, 2507L, 2508L, 2509L, 2510L, 
2511L, 2512L, 2513L, 2514L, 2515L, 2516L, 2517L, 2518L, 2519L, 
2520L, 2521L, 2522L, 2523L, 2524L, 2525L, 2526L, 2527L, 2528L, 
2529L, 2530L, 2531L, 2532L, 2533L, 2534L, 2535L, 2536L, 2537L, 
2538L, 2539L, 2540L, 2541L, 2542L, 2543L, 2544L, 2545L, 2546L, 
2547L, 2548L, 2549L, 2550L, 2551L, 2552L, 2553L, 2554L, 2555L, 
2556L, 2557L, 2558L, 2559L, 2560L, 2561L, 2562L, 2563L, 2564L, 
2565L, 2566L, 2567L, 2568L, 2569L, 2570L, 2571L, 2572L, 2573L, 
2574L, 2575L, 2576L, 2577L, 2578L, 2579L, 2580L, 2581L, 2582L, 
2583L, 2584L, 2585L, 2586L, 2587L, 2588L, 2589L, 2590L, 2591L, 
2592L, 2593L, 1L, 2L, 3L), class = "data.frame")

tail(df)

           Date Data
2591 2021-11-25 3.43
2592 2021-11-26 3.40
2593 2021-11-29 3.33
2594 2021-12-15 3.33
2595 2021-12-15 4.01
2596 2021-12-15 2.50

As you can see from the tail of the dataset, the last 3 observations occur in the same day. What I would like to do is to plot those observations at the end of last observation (2021-11-29) as 3 distinct scenarios.

This is the simple code for the simple ggplot time series without the last 3 obs:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library("ggplot2")

ggplot(df[-c(2594:2596),], aes(x = Date, y = Data)) +
geom_line(size = 1, color = "blue") +
labs(x="", y="") +
theme_classic() +
theme(panel.border = element_rect(fill=NA),legend.title = element_blank()) +
theme(text = element_text(size=20)) 

However, I would like to get something like this (forgive my paint skills):

https://i.stack.imgur.com/NQARJ.png

With the last 3 observations as distinct scenarios with different colours and annotations.

Can anyone help me with that?

Thanks!

>Solution :

One way would be to split the data frame into an observation_df and scenario_df. The first is equivalent to yours above, while the scenario_df we group each of the 3 scenarios with the same starting date and value (end of observation_df) and the 3 separate scenarios.

library(dplyr)
library(ggplot2)

first_df <- filter(df, Date != "2021-12-15")
scenario_df <- filter(df, Date == "2021-12-15") %>%
  rowid_to_column() %>%
  bind_rows(data.frame(
    rowid = 1:3,
    Date = as.Date("2021-11-29"),
    Data = first_df[first_df$Date == "2021-11-29", "Data"]
  ))

scenario_df
#>   rowid       Date Data
#> 1     1 2021-12-15 3.33
#> 2     2 2021-12-15 4.01
#> 3     3 2021-12-15 2.50
#> 4     1 2021-11-29 3.33
#> 5     2 2021-11-29 3.33
#> 6     3 2021-11-29 3.33

Then we can just add a single line to your plot above to get the separate scenarios plotted.

ggplot(first_df, aes(x = Date, y = Data)) +
  geom_line(size = 1, color = "blue") +
  labs(x="", y="") +
  theme_classic() +
  theme(panel.border = element_rect(fill=NA),legend.title = element_blank()) +
  theme(text = element_text(size=20)) +
  geom_line(aes(group = rowid,
                color = rowid),
            data = scenario_df)

Example plot of scenarios

You can create annotations, adjust the visualisation, and otherwise finalise the image easily from there.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading