I see. you are trying to emulate the behavior of ggplot.
My suggestion would be to write a function that accepts a data-frame as input, does calculations, and produces a plot.
p = plot()
gd = groupby(df, :groupvar)
for sdf in gd
sp = analysis_and_plot(sdf)
plot!(p, sp)
end
But there are other alternatives. You may want to perform the calculation using by and keep your data in long format, then use StatsPlots to emulate ggplor grammar of graphics. Something like
by(df, :groupvar) do sdf
analysis(sdf) # produces a data frame
end