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[2] - x[1]
  return length.value, x[1] + length/2

ml = mid_length.(recessions)

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


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

Yeah sorry it’s called vspan, added in . I mixed it up with the Makie term.

I have encountered some weird behaviour and it would be great, if someone could help me understand it.

using Plots, Dates

x   = Dates.Date(1960,1,1):Dates.Month(1):Dates.Date(2020,5,1)
plt = plot(x, randn(length(x)), alpha = 0.3, label = "")

recs = [Dates.Date(1960,04,1):Dates.Month(1):Dates.Date(1961,02,1),

for t in 1:8
    vspan!(plt, recs[t], label = "", color = :grey)


Which results in the following plot:


The following things seem to be weird: First, the grey bars look like that there are multiple ones that overlap, although it is just one recession that I specified. Second, the y-axis seems to be converted to something with a date-structure, but I did not intend that. Third, the bars are too thin and do not cover all of the period specified. What can I do about that? Is there a way to go about it using vspan?