First light
In which I take Makie.jl for a spin to visualize the Palmer Penguins. First challenge: how to color the species.
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
activate!()
CairoMakie.= scatterlines(1:10, 1:10)
fig 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:
= PalmerPenguins.load()
table DataFrame(table)
end
# load the data into a DataFrame and drop missing data
# (let's keep this simple)
begin
= load_penguins()
penguins_raw = dropmissing!(penguins_raw)
penguins end
Now create a plotting function1:
function penguin_plot_simple()
= Figure(;
fig =(600,400),
size
)= Axis(fig[1,1];
ax ="Body mass",
xlabel="Flipper length",
ylabel="Palmer Penguins",
title
)= scatter!(penguins[!, :body_mass_g], penguins[!, :bill_length_mm])
plotobj
figend
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"
= categorical(penguins.species)
penguins.species
# get integer codes for spcies (note the broadcasting operator '.' after levelcode):
= levelcode.(penguins.species)
penguins.species_code
# look at the result in the two relevant columns:
:species, :species_code]]
penguins[!, [end
Finally, we can map color to the :species_code
column:
function penguin_plot()
= Figure(;
fig =(600,400),
size
)= Axis(fig[1,1];
ax ="Body mass",
xlabel="Flipper length",
ylabel="Palmer Penguins",
title
)scatter!(penguins[!, :body_mass_g], penguins[!, :bill_length_mm];
= penguins[!, :species_code],
color =:Dark2_3
colormap
)
figend
Great! There’s still much to do (legend, theme, maybe some annotations and formatting…) which I will leave open for later posts.
Footnotes
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
@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}
}