MakieOrg / Makie.jl

Interactive data visualizations and plotting in Julia
https://docs.makie.org/stable
MIT License
2.43k stars 312 forks source link

`boxplot` simplification + colors #1363

Open SimonEnsemble opened 3 years ago

SimonEnsemble commented 3 years ago

seems to me boxplot could be simplified by taking in an array of arrays?

boxplot([rand(30), rand(50), rand(10)])

then e.g. plotting three boxes for the petal length of the iris data set, grouped by species, is as easy as:

boxplot([df_iris_species[:, :PetalLength] for df_iris_species in groupby(df_iris, :Species)])

right now it's not too trivial to make this.

another thing: seems I cannot make the boxes three different colors, unless I use dodge.

full code needed to see petal length distributions grouped by species:

using RDataSets

df_iris = dataset("datasets", "iris")
species_to_position = Dict(zip(unique(df_iris[:, :Species]), 1:3))

petal_lengths = zeros(nrow(df_iris))
positions = zeros(Int, nrow(df_iris))
for (i, row) in enumerate(eachrow(df_iris))
    petal_lengths[i] = row[:PetalLength]
    positions[i] = species_to_position[row[:Species]]
end
# maybe a flatten or list comprehension to do this more easily using the groupby

fig5 = Figure()
ax5 = Axis(fig5[1, 1], 
title="irises", 
      ylabel="petal length",
xticks=(1:3, unique(df_iris[:, :Species]))
)
# comment shows what I wish I could do
boxplot!(positions, petal_lengths)#, color=ColorSchemes.Accent_3)
current_figure()
SimonEnsemble commented 3 years ago

ohhhh, I see how now. lmk if a PR is welcome with an example like this in the docs.... :smile:

for (i, df_by_species) in enumerate(groupby(df_iris, :Species))
    boxplot!(i * ones(nrow(df_by_species)), df_by_species[:, :PetalLength])
end