# Arrange bar plot by frequency in AlgebraofGraphics.jl

**URL:** <https://discourse.julialang.org/t/arrange-bar-plot-by-frequency-in-algebraofgraphics-jl/121506>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 20, 2024, 2:54pm UTC](https://discourse.julialang.org/t/arrange-bar-plot-by-frequency-in-algebraofgraphics-jl/121506 "2024-10-20T14:54:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nithin\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nithin_m/32/16811_2.png) [@Nithin\_M](https://discourse.julialang.org/u/Nithin_M)\
**Post date:** [October 20, 2024, 2:54pm UTC](https://discourse.julialang.org/t/arrange-bar-plot-by-frequency-in-algebraofgraphics-jl/121506/1 "2024-10-20T14:54:18Z")

</div>

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

````julia
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()

````

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [October 21, 2024, 2:06pm UTC](https://discourse.julialang.org/t/arrange-bar-plot-by-frequency-in-algebraofgraphics-jl/121506/2 "2024-10-21T14:06:06Z")

</div>

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

```julia
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) 

```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 21, 2024, 2:36pm UTC](https://discourse.julialang.org/t/arrange-bar-plot-by-frequency-in-algebraofgraphics-jl/121506/3 "2024-10-21T14:36:39Z")

</div>

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

> **[Pre-sorted data · Algebra of Graphics](https://aog.makie.org/stable/gallery/gallery/data%20manipulations/presorted_data/)**
>
> Documentation for Algebra of Graphics.

---

<div class="post-metadata">

**Author:** ![Nithin\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nithin_m/32/16811_2.png) [@Nithin\_M](https://discourse.julialang.org/u/Nithin_M)\
**Post date:** [October 22, 2024, 5:32am UTC](https://discourse.julialang.org/t/arrange-bar-plot-by-frequency-in-algebraofgraphics-jl/121506/4 "2024-10-22T05:32:22Z")

</div>

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