Hello,
I am trying to create a grid plot with the following code. The individual plots show the legends. However, when I put the two plots together in a grid, the legend disappears. How can I retain the legends for the grid? Thanks.
## Create a figure grid
fig_plot = Figure(resolution = (1000, 500); fontsize = 20)
draw!(fig_plot[1,1], plt_iv;
axis=(; aspect=1,
title="Average Concentration - IV Route",
xlabel="Time (hour)",
ylabel="Concentration (mcg/L)",
#yscale = log10,
xticks = ([0,2,4,6,8,12,24]),),
)
draw!(fig_plot[1,2], plt_oral; axis=(; aspect=1,
title="Average Concentration - Oral Route",
xlabel="Time (hour)",
ylabel="Concentration (mcg/L)"),
)
fig_plot
These are the individual plots with the legends.
@MathangiCTM when you call draw!
you need to add the legends manually.
If you go to the AlgebraOfGraphics
(AoG) documentation on Legend tweaking · Algebra of Graphics,
you’ll see that you can use the legend!
function that accepts two positional arguments:
figpos
: figure position, e.g. f[1, 1]
, for where to draw the legend; and
grid
: output of draw!
.
It also accepts all of the keyword arguments from Makie’s Legend
(Legend · Makie).
I am able to introduce your desired behavior in this MWE.
See if this answer your questions:
using AlgebraOfGraphics
using CairoMakie
using PharmaDatasets
df = dataset("demographics_1")
plt =
data(df) *
mapping(
:AGE => "Age in years",
:eGFR => log => "eGFR (log scaled)";
color = :ISMALE => renamer([0 => "Female", 1 => "Male"]) => "Sex",
) *
visual(Scatter)
f = Figure(resolution = (1000, 500); fontsize = 20)
g1 = draw!(
f[1, 1],
plt;
axis = (;
title = "Title 1",
xlabel = "X-Label",
ylabel = "Y-Label",
),
)
g2 = draw!(
f[1, 2],
plt;
axis = (;
title = "Title 2",
xlabel = "X-Label",
ylabel = "Y-Label",
),
)
legend!(f[0, 1:2], g2; tellheight=true, tellwidth=true, halign=:center, valign=:top, titleposition=:left, orientation=:horizontal)
f
Note that I am using some Makie Legend
s keyword arguments in legend!
.
This produces the following image:
@storopoli Thank you. This is helpful. In your example, the legend was common between the two graphs. If the legends are different and specific for each graph, is there a way to get the legends on top of the respective graphs (and at the center of the graphs) ?
In my case, I wrote two legend statements and aligned it either right or left like this.
legend!(fig_plot[0,1:2], g1; tellheight = true,
tellwidth=true,halign = :left,valign=:top,
titleposition=:left, orientation=:horizontal, framevisible=false)
legend!(fig_plot[0,1:2], g2; tellheight = true,tellwidth=true,halign = :right,valign=:top, titleposition=:left, orientation=:horizontal)
Thanks.
Yes, check this updated MWE:
using AlgebraOfGraphics
using CairoMakie
using PharmaDatasets
df = dataset("demographics_1")
plt1 =
data(df) *
mapping(
:AGE => "Age in years",
:eGFR => log => "eGFR (log scaled)";
color = :ISMALE => renamer([0 => "Female", 1 => "Male"]) => "Sex",
) *
visual(Scatter)
plt2 =
data(df) *
mapping(
:AGE => "Age in years",
:eGFR => log => "eGFR (log scaled)";
color = :ISMALE => renamer([0 => "F", 1 => "M"]) => "Sex",
) *
visual(Scatter)
f = Figure(resolution = (1000, 500); fontsize = 20)
g1 = draw!(
f[1, 1],
plt1;
axis = (;
title = "Title 1",
xlabel = "X-Label",
ylabel = "Y-Label",
),
)
g2 = draw!(
f[1, 2],
plt2;
axis = (;
title = "Title 2",
xlabel = "X-Label",
ylabel = "Y-Label",
),
)
legend!(f[0, 1], g1; tellheight=true, tellwidth=false, halign = :left,valign=:top,
titleposition=:left, orientation=:horizontal, framevisible=false)
legend!(f[0, 2], g2; tellheight=true, tellwidth=false, halign = :left,valign=:top,
titleposition=:left, orientation=:horizontal, framevisible=false)
f
1 Like