Shadowing your ggplot lines. Forecasting confidence interval use case.

Plot your confidence interval easily with R! With ggplot geom_ribbon() you can add shadowed areas to your lines. We show you how to deal with it!

R ggplot2 plot of the forecast(in red) and it's condidence intervals(in blue) using geom_ribbon function.


Yesterday I was asked to easily plot confidence intervals at ggplot2 chart. Then I came up with this shadowing ggplot2 feature called geom_ribbon().

It’s not a trivial issue as long as you need to gather your data in order to achieve a tidy format. When you already have this data frame, all you need is geom_ribbon().

By using the following commented code you are able to show not only your point estimated forecast but also its confidence or prediction intervals.

For a multi-line plot, you should include the colour and group aesthetic as follows:


You can see related R visualizations and tips on TypeThePipe


Carlos Vecina
Carlos Vecina
Data Scientist & TechLead at Bankinter

Data Scientist & TechLead at Bankinter | AI & Data Science for Business