# Small-multiples plots in Plots.jl

**URL:** <https://discourse.julialang.org/t/small-multiples-plots-in-plots-jl/83529>\
**Category:** General Usage\
**Tags:** plotting, statsplots\
**Created:** [June 29, 2022, 5:33pm UTC](https://discourse.julialang.org/t/small-multiples-plots-in-plots-jl/83529 "2022-06-29T17:33:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 29, 2022, 5:33pm UTC](https://discourse.julialang.org/t/small-multiples-plots-in-plots-jl/83529/1 "2022-06-29T17:33:44Z")

</div>

I have time series for each state in the US. I have grouped the states by a geographic grouping factor. There are 8 state groups.

I’d like to make 8 panels, one for each state group, within the panel I want each state time series to be its own colored timeseries.

Imagine I have a dataframe like:

```julia
df = DataFrame(statecode = ["AK","AK",..."CA","CA"...], year = [...], stategroup = [1,1,...,2,2,...], measure = [...])

```

and I want something kinda like:

```julia
@df df plot(:year,:measure,group=(:stategroup,:statecode), layout=length(unique(df.stategroup)))

```

Except when I do that it just mixes states willy nilly not each group in its own panel.

How do I get that sort of thing?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 29, 2022, 6:10pm UTC](https://discourse.julialang.org/t/small-multiples-plots-in-plots-jl/83529/2 "2022-06-29T18:10:29Z")

</div>

Please check if this may help:

```julia
using DataFrames, StatsPlots

df = DataFrame(statecode=["AK","AK","CA","CA","TX","TX"], year=6:-1:1, stategroup=[1,1,2,2,1,1], measure=1:6)
N = length(unique(df.stategroup))
ylims = extrema(df.measure)
fig = plot(layout=N, legend=:topright)
gdf = groupby(df,:stategroup)
for i in 1:N
    @df gdf[i] plot!(fig[i],:year,:measure, group=(:stategroup, :statecode), ylims=ylims)
end
fig

```

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [June 29, 2022, 8:21pm UTC](https://discourse.julialang.org/t/small-multiples-plots-in-plots-jl/83529/3 "2022-06-29T20:21:13Z")

</div>

ok, I think this is similar to what I wound up trying which was a list comprehension:

```julia
plot([plot(gr.year,gr.crudedeathrate ./ gr.baseline,group=gr.STUSAB,legend=:topleft,ylim=(0.0,2.5)) 
    for gr in groupby(bljoined,:stgroup)]...; size=(1000,1000),lineweight=4,xlab="Year",ylab="Relative Rate")

```
