How to use AlgebraOfGraphics to plot three figures together

Hello, the current plots I have are as follows:


These are three different figures. I just paste them together. I want to have them in one figure.
This is my code:

using AlgebraOfGraphics, CairoMakie
gdfc
data(gdfc)
draw(AlgebraOfGraphics.data(gdfc) * mapping(:date, :nrow, color=:wave) * visual(Scatter);
axis = (
    xlabel = "Date",
    ylabel = "Number of Participants",
    title = "Survey Waves and Number of Participants",
    backgroundcolor = :ghostwhite
),
figure = (resolution = (1000, 300), backgroundcolor = :ghostwhite, )
) 


draw(AlgebraOfGraphics.data(covid_death) * mapping(:date, :new_deaths_smoothed_per_million) * visual(Lines);
axis = (
    xlabel = "Date",
    title = "Number of New Deaths per Million",
    backgroundcolor = :ghostwhite
),
figure = (resolution = (1000, 300), backgroundcolor = :ghostwhite, )
)

draw(AlgebraOfGraphics.data(covid_death) * mapping(:date, :stringency_index) * visual(Lines);
axis = (
    xlabel = "Date",
    title = "Stringency Index",
    backgroundcolor = :ghostwhite
),
figure = (resolution = (1000, 300), backgroundcolor = :ghostwhite, )
)

I also tried to use CairoMakie to plot it. This is what I got.

However, this plot is inaccurate in the sense of survey periods.
Here are my codes to plot the inaccurate figure above.

# check the interested variables
covid_death.date
covid_death.new_deaths_smoothed_per_million 
covid_death.stringency_index

# plot the figure with grey background
f = Figure(backgroundcolor = RGBf(0.98, 0.98, 0.98), size = (1000, 900));

# Setting up GridLayouts
ga = f[1, 1] = GridLayout()
gb = f[2, 1] = GridLayout()
gc = f[3, 1] = GridLayout()
f

# Panel A: daily new deaths per million
ax = CairoMakie.Axis(ga[1,1])
days = length(covid_death.date)
lines!(ax, 1:days, covid_death.new_deaths_smoothed_per_million, color = :red)
ax.xticks = (1:30:days)
# ax.xticks = (1:30:days, string.(covid_death.date)[1:30:days])

f


# Panel B: stringency_index
bx = CairoMakie.Axis(gb[1,1])
days = length(covid_death.date)
covid_death.stringency_index
lines!(bx, 1:days, covid_death.stringency_index, coloer = :blue)
bx.xticks = (1:30:days)
f

# Panel C: survey period and number of participants
names(data_reshaped)
data_reshaped
describe(data_reshaped)
data_reshaped.submitdate
# transform the dataset

# add the date column, one day before the submitdate
data_reshaped.submitdate = Dates.DateTime.(data_reshaped.submitdate , "yyy-mm-dd HH:MM:SS")
data_reshaped.date = Dates.Date.(data_reshaped.submitdate .- Dates.Day(1))
 
# build the third panel
using CategoricalArrays
gdf = groupby(data_reshaped, [:date;:wave]) # GroupedDataFrame with 1702 groups based on key: token
gdfc = combine(gdf, nrow) # 365×3 DataFrame
gdfc.wavec = categorical(gdfc.wave, levels = ["base", "fup1", "fup2"])
levels(gdfc.wavec)
fieldnames(typeof(gdfc.wavec))
gdfc.wavecindex = recode(unwrap.(gdfc.wavec), "base"=>1, "fup1"=>2, "fup2"=>3)
gdfc
myDiscColorScale = [colorant"orange",colorant"purple",colorant"cyan"]
[gdfc.wavecindex]
gdfc.mycolor = myDiscColorScale[gdfc.wavecindex]
gdfc

cx = CairoMakie.Axis(gc[1,1])
CairoMakie.scatter!(cx, gdfc.date, gdfc.nrow, color = gdfc.mycolor)

f

Thank you!

If I get the issue correct, you want to link the X-axis across the 3 plots? Is that right?

Yes, that is correct. I used Makie to link the x-axis of three plots. I got this figure.

The third plot still has a problem with the x-axis. This is because not every day during the survey period was recorded. For example, if there were nobody submitted the survey, that day would be dropped. However since Makie took the length of the time as the x-axis instead of the real date, the distribution of the third plot’s x-axis is weird.

I am using AlgebraOfGraphics to work on the final plot.

I think that the easiest way would be to use the same xticks keyword argument in all axis in AoG.

1 Like