 Showing shaded areas in StatPlots

Hi everyone. I have a time-series dataset which I’ve plotted in Julia using StatPlots.jl, and now would like to impose these grey bars on the plot to emphasize certain periods of time (such as wars/recessions etc.), I’ve attached an image below as an example, I’m using the same format (i.e. date on the x-axis, and some time-series measure on the y-axis). I’m trying to do so using the bar command. I’d like these grey areas to show up in periods 24-28, and periods 85-86 for example. I’ve attached my pseudo-code here:

plt1 = bar((24:28), (85-86), fill=:lightgrey, label= "War")

However, this plots only the y-values as such. I’ve also tried a few variations on bar_width (but not sure how to specify at which periods i.e. values on my x-axis I’d want them to be) and x=(24:28) but this returns the error that the y-value should be equal to the x-value or greater only by 1.

Would appreciate all help, thank you.

you seem to have forgotten to attach the image

just realised that, sorry! attached now.

There’s a vbox! command to add those

Is that in Plots.jl? I can’t find this in the docs or via Google (all results point to Makie instead).

The idea in the original post could be implemented like this, assuming you get your recession dates as start date and end date:

using TimeSeries, StatsPlots

dates = Date(2018, 1, 1):Day(1):Date(2018, 12, 31)

ta = TimeArray(dates, rand(length(dates)))

recessions = [(Date(2018, 4, 1), Date(2018, 5, 1))
(Date(2018, 7, 1), Date(2018, 9, 1))]

function mid_length(x)
length = x - x
return length.value, x + length/2
end

ml = mid_length.(recessions)

p = plot(ta)
for r in ml
bar!(p, [r], , width = r, color = "grey", alpha = 0.3, label = "")
end

display(p)

I seem to be off by a day or so so this can probably be refined:

Yeah sorry it’s called vspan, added in https://github.com/JuliaPlots/Plots.jl/pull/1445 . I mixed it up with the Makie term.