Make transparent square in Gadfly

I want to plot a transparent square over certain time ranges in the Gantt chart @Mattriks created here.

It looks like this:
image

And I want it to look somtehing like this:
ejemplo-gadfly

How could I do it?

You can add layers with Geom.bar and alpha. Like so

using DataFrames
using Dates
using Gadfly

D1 = DataFrame(y = Int[1, 2, 3, 4],
    ylab=["Collect data", "Clean data", "Analyze data", "Write report"],
    x = Date.(["2017-04-01", "2018-04-01", "2018-06-01", "2019-04-01"]),
    xend = Date.(["2018-04-01", "2018-06-01", "2019-04-01", "2020-04-01"]),
    id = ["a","b","b","c"]
)

ylabdict = Dict(i=>D1[!,:ylab][i] for i in 1:4)
coord = Coord.cartesian(ymin=0.4, ymax=4.6)

D2 = DataFrame(y=Int[5],
    xmin=Date.(["2017-01-01"]), xmax=Date.(["2017-06-01"])
)
D3 = DataFrame(y=Int[5],
    xmin=Date.(["2018-06-01"]), xmax=Date.(["2018-12-31"])
)

l2 = layer(D2, xmin=:xmin, xmax=:xmax, y=:y, Geom.bar, alpha=[0.2], Theme(default_color="white"), order=1)
l3 = layer(D3, xmin=:xmin, xmax=:xmax, y=:y, Geom.bar, alpha=[0.2], Theme(default_color="white"), order=1)

p = plot(D1, coord, 
    layer(x=:x, xend=:xend, y=:y, yend=:y, color=:id, Geom.segment, Theme(line_width=10mm)), 
    Scale.y_continuous(labels=i->get(ylabdict,i,"")),
    Guide.xlabel("Time"), Guide.ylabel(""),
    Theme(key_position=:none), l2, l3
)

Then you get the following image

2 Likes

Thanks! That’s exactly what I wanted!

An update to the above code:

D1 = DataFrame(
    ylab=["Collect data", "Clean data", "Analyze data", "Write report"],
    x=Date.(["2017-04-01", "2018-04-01", "2018-06-01", "2019-04-01"]),
    xend=Date.(["2018-04-01", "2018-06-01", "2019-04-01", "2020-04-01"]),
    id=["a","b","b","c"]
)

D2 = DataFrame(xmin=Date.(["2017-01-01", "2018-06-01"]), 
    xmax=Date.(["2017-07-01", "2018-12-31"]))
l2=layer(D2, xmin=:xmin, xmax=:xmax, Geom.vband, color=[colorant"red"], alpha=[0.2])

p = plot(D1, Scale.y_discrete,
    layer(x=:x, xend=:xend, y=:ylab, yend=:ylab, color=:id, Geom.segment, 
        Theme(line_width=10mm)), l2,
    Guide.xlabel("Time"), Guide.ylabel(""), Theme(key_position=:none))

Gantt

See Geom.band in the Gadfly docs

2 Likes

Great! Thanks!