First light

In which I take Makie.jl for a spin to visualize the Palmer Penguins. First challenge: how to color the species.

Capulet's Orchard
JuliaLang
miniblog
Author
Published

June 26, 2025

One of my greatest interests in Julia – at least for now – is how to visualize data. As recommended by the book, I’m following, I’ll use Makie.jl. As discussed in my previous post, please excuse the missing text-output.

begin
    using CairoMakie
    CairoMakie.activate!()
    fig = scatterlines(1:10, 1:10)
end

This produces the following simple chart:

The next logical step, coming from the ggplot2-verse was to do something with the Palmer Penguins dataset. This is also available in Julia via the PalmerPenguins.jl package.

# load required packages
begin
    using DataFrames
    using PalmerPenguins
end
# setup a function to load the data
function load_penguins()
    # in Pluto.jl I had to set my linux's environment variable 
    # DATADEPS_ALWAYS_ACCEPT=true before running the next line:
    table = PalmerPenguins.load()
    DataFrame(table)
end
# load the data into a DataFrame and drop missing data 
# (let's keep this simple)
begin
  penguins_raw = load_penguins()
  penguins = dropmissing!(penguins_raw)
end

Now create a plotting function1:

function penguin_plot_simple()
    fig = Figure(;
        size=(600,400),
        )
    ax = Axis(fig[1,1];
        xlabel="Body mass",
        ylabel="Flipper length",
        title="Palmer Penguins",
        )
    plotobj = scatter!(penguins[!, :body_mass_g], penguins[!, :bill_length_mm])
    fig
end

That’s good start. As last step of this miniblog, I struggled with mapping color to the species column. In ggplot2 I would simply map it with aes(color = species), but this failed in Makie.jl. Instead I need to convert the species to a numerical category type, which Makie.jl can use to map color:

begin
  # we need this new package:
    using CategoricalArrays
    
    # make sure the species column is of type "category"
    penguins.species = categorical(penguins.species)
    
    # get integer codes for spcies (note the broadcasting operator '.' after levelcode):
    penguins.species_code = levelcode.(penguins.species)
    
    # look at the result in the two relevant columns:
    penguins[!, [:species, :species_code]]
end

Finally, we can map color to the :species_code column:

function penguin_plot()
    fig = Figure(;
        size=(600,400),
        )
    ax = Axis(fig[1,1];
        xlabel="Body mass",
        ylabel="Flipper length",
        title="Palmer Penguins",
        )
    scatter!(penguins[!, :body_mass_g], penguins[!, :bill_length_mm];
                       color = penguins[!, :species_code],
                       colormap=:Dark2_3
                      )
    fig
end

Great! There’s still much to do (legend, theme, maybe some annotations and formatting…) which I will leave open for later posts.

Footnotes

  1. The Julia Data Science book uses functions extensively, even for single plots. In R I have mostly seen this in cases where parametrised plots were generated, but I like the concept, so I’ll follow this recommendation for Julia.↩︎

Reuse

Citation

BibTeX citation:
@misc{gebhard2025,
  author = {Gebhard, Christian},
  title = {First Light},
  date = {2025-06-26},
  url = {https://christiangebhard.com/posts/2025-06-26-julia-CO-02/},
  langid = {en}
}
For attribution, please cite this work as:
Gebhard, Christian. 2025. “First Light.” June 26, 2025. https://christiangebhard.com/posts/2025-06-26-julia-CO-02/.