Arrange bar plot by frequency in AlgebraofGraphics.jl

I want to have a bar plot ordered by species frequency using AlgebraOfGraphics.jl.

using PalmerPenguins, DataFrames, AlgebraOfGraphics, CairoMakie, CategoricalArrays, Chain

# Load and clean the data
penguins = @chain PalmerPenguins.load() begin
    DataFrame
    dropmissing
end


# Create the plot layer with frequency
penguin_layer = data(penguins) * frequency() * mapping(:species)
draw(penguin_layer)

in R i could do somthing like this

```{=r}
library(tidyverse)
library(palmerpenguins)

penguins |>
drop_na()|>
ggplot()+
aes(x=fct_infreq(species))+
geom_bar()

There may well be a more elegant way, but here is one that works:

using Random;
df = DataFrame(:species => rand(["a", "b", "c"], 50));

gdf = groupby(df, :species);
df2 = combine(gdf, proprow => :fraction);
sort!(df2, :fraction);

plt = data(df) * frequency() * mapping(:species => sorter(df2.species))
draw(plt) 
1 Like

You can use :species => presorted now, I think if you had a vector with multiple of the same entries, your solution would not work.

Thanks. is there a way to control size/width of the bars?