Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facet vs layout #42

Closed
greimel opened this issue May 1, 2020 · 1 comment · Fixed by #46
Closed

Facet vs layout #42

greimel opened this issue May 1, 2020 · 1 comment · Fixed by #46

Comments

@greimel
Copy link
Collaborator

greimel commented May 1, 2020

Probably faceting and layouting could be two different options in this package. Faceting is for grouping data, layouting for plotting different variables (as in your example in the docs)

First, layouting.

N = 500
x = rand(N)
gx = rand(["x grp 1","x grp 2", "x grp 3"], N)
gy = rand(["y grp 1","y grp 2"], N)

df = DataFrame(g = g, x = x, y = (g .== "very") .+ x .+ randn(N), gx=gx, gy=gy)

cols = style(:x, :y);
grp = style(layout_x=:gx, layout_y=:gy)
scat = spec(Scatter)
pipeline = cols * scat
aog = data(df) * grp * pipeline

layout

Now faceting, in the ggplot sense.
facet

scene, layout = MakieLayout.layoutscene(fontsize=35, font="CMU Serif roman", resolution=round.(Int, (2 * 600, 2 * 540)))
layout.alignmode = Outside(0, 25, 0, 5)

AlgebraOfGraphics.layoutplot!(scene, layout, aog)
AbstractPlotting.save(joinpath("figures/layout.pdf"), scene)

x_facet_titles = sort!(unique(gx))
y_facet_titles = sort!(unique(gy))

grd_lay = layout.content[1].content

# remove xlabel and ylabel
for i in 1:length(grd_lay.content)
  ax = grd_lay.content[i].content
  ax.xlabelvisible[] = length(x_facet_titles) == 0
  ax.ylabelvisible[] = length(y_facet_titles) == 0
end

# add categories of x_facet variable and linked x_label
if length(x_facet_titles) > 0
  lax11 = grd_lay.content[1].content
  n = size(grd_lay)[1]
  for (i,t) in enumerate(x_facet_titles)
    text = LText(scene, t)
    text.tellheight = true
    text.tellwidth = false
    grd_lay[1,i,Top()] = LRect(scene, color = RGBAf0(0,0,0,0.2), strokevisible=false) 
    grd_lay[1,i,Top()] = text
  end
  
  xlabel_facet = LText(scene, string(lax11.xlabel[]))
  xlabel_facet.tellheight = true
  xlabel_facet.tellwidth = false
  grd_lay[end+1,:] = xlabel_facet
end

# add categories of y_facet variable and linked y_label
if length(y_facet_titles) > 0
  lax11 = grd_lay.content[1].content
  n = size(grd_lay)[2]
  for (i,t) in enumerate(y_facet_titles)
    text = LText(scene, t, rotation=π/2)
    text.tellheight = false
    text.tellwidth = true
    grd_lay[i,n, Right()] = LRect(scene, color = RGBAf0(0,0,0,0.2), strokevisible=false) 
    grd_lay[i,n, Right()] = text
  end
  
  ylabel_facet = LText(scene, string(lax11.ylabel[]), rotation = π/2)
  ylabel_facet.tellheight = false
  ylabel_facet.tellwidth = true
  grd_lay[1:end-1,0] = ylabel_facet
end
@greimel greimel mentioned this issue May 6, 2020
@asinghvi17
Copy link
Member

asinghvi17 commented May 6, 2020

It may be worth it to increase the padding between axes in the last plot, through colgap!.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants