Gadfly.jl icon indicating copy to clipboard operation
Gadfly.jl copied to clipboard

subplot_grid - cols option?

Open TransGirlCodes opened this issue 8 years ago • 4 comments

Hi, it's not obvious from the documentation, but I was wondering if subplot_grid has an ncol option in the same way as ggplot2's facet_wrap? I have many groups for grouping the x axis, and it lays all the subplots out horizontally which squashes them, I'm looking for a way to make it split the row of subplots into multiple rows to avoid the squashing.

Thanks, Ben.

TransGirlCodes avatar Jun 16 '16 12:06 TransGirlCodes

I don't think a facet_wrap has been implemented in Gadfly. But here is an example (also see #841 for another way).

using DataFrames
x = linspace(-2, 2, 11)
f(a) = a*x+5.0*randn(11)
# First make some data, without indices:
D1 = vcat([DataFrame(x=x, y=f(a)) for a in linspace(2,16,8) ]...)
# Now make some col and row indices:
D2 = vcat([DataFrame(x=x, xg=col, yg=row)  for col in 1:4, row in 1:2  ]...)
# and concatenate:
D = hcat(D1,D2[:,2:3])

coord = Coord.cartesian(ymin=-50, ymax=50)
theme(x) = Theme(default_color=parse(Colors.Colorant, x))

 p = plot(D, xgroup=:xg, ygroup=:yg,
    Geom.subplot_grid(coord,
        layer(x=:x, y=:y, Geom.point, order=1),
        layer(x=:x, y=:y, Geom.smooth(method=:lm), theme("red"), order=2)
    ),
    Guide.xlabel("x"),
    Guide.ylabel("y")
)

You can stop the group labels from appearing by moving xgroup= and ygroup= into each layer.

Mattriks avatar Jun 16 '16 13:06 Mattriks

In case this isn't flexible enough for you, it's pretty straightforward in Plots:

ys = map(f,linspace(2,16,8))
scatter(x, ys, layout=(2,4), smooth=true, link=:all, lc=:red)

tmp

Note: you probably want to grab the master branch as I haven't tagged some bug fixes.

tbreloff avatar Jun 16 '16 14:06 tbreloff

I'm looking for how to do the equivalent of a facet_wrap in ggplot2 or seaborn, and this thread seems like the closest match for Gadfly. I'm guessing the prefered solution now is to put all individual plots into a vector, then reshape & plot with gridstack...? Do let me know if there's a more idiomatic solution. Thanks!

tbenst avatar Feb 21 '21 23:02 tbenst

Nope, no facet_wrap yet. Currently changing some internal code (#1520) which might make the development of subplot_wrap easier.

Mattriks avatar Feb 21 '21 23:02 Mattriks