Skip to content

Commit a1dea3c

Browse files
authored
Label metadata (#660)
Makes it more convenient to work with table data that has column label metadata attached: ```julia using AlgebraOfGraphics using CairoMakie using DataFrames using Unitful df = DataFrame( t = 1:10, v = sin.(1:10), c = sqrt.(1:10) .* u"mg/L", ) colmetadata!(df, :t, "label", "Time") colmetadata!(df, :v, "label", "Volume") colmetadata!(df, :c, "label", "Concentration") data(df) * mapping(:t, :v, color = :c) * visual(Scatter) |> draw ``` <img width="588" alt="image" src="https://github.com/user-attachments/assets/4e48621c-dbff-4c0f-ac14-e0f871a21b75" />
1 parent 8e8275c commit a1dea3c

File tree

6 files changed

+46
-2
lines changed

6 files changed

+46
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- If available, column metadata with the key `"label"` will be used instead of the column name for determining a mapping's label [#660](https://github.com/MakieOrg/AlgebraOfGraphics.jl/pull/660).
6+
57
## v0.10.5 - 2025-05-14
68

79
- Added a new `AesLineWidth` aesthetic for `Lines`, `ScatterLines`, `HLines` and `VLines` [#656](https://github.com/MakieOrg/AlgebraOfGraphics.jl/pull/656).

Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.10.5"
66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
88
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
9+
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
910
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1011
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
1112
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
@@ -38,6 +39,7 @@ AlgebraOfGraphicsUnitfulExt = "Unitful"
3839
[compat]
3940
Accessors = "0.1"
4041
Colors = "0.12, 0.13"
42+
DataAPI = "1"
4143
Dictionaries = "0.3, 0.4"
4244
DynamicQuantities = "1"
4345
FileIO = "1.1"
@@ -63,6 +65,7 @@ julia = "1.6"
6365

6466
[extras]
6567
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
68+
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
6669
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
6770
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
6871
ImageInTerminal = "d8c32880-2388-543b-8c61-d9f865259254"
@@ -73,4 +76,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
7376
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
7477

7578
[targets]
76-
test = ["Random", "Shapefile", "Statistics", "Test", "CairoMakie", "DelimitedFiles", "ImageInTerminal", "Unitful", "DynamicQuantities"]
79+
test = ["Random", "Shapefile", "Statistics", "Test", "CairoMakie", "DataFrames", "DelimitedFiles", "ImageInTerminal", "Unitful", "DynamicQuantities"]

src/AlgebraOfGraphics.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ using KernelDensity: kde, pdf
2323
using StatsBase: fit, histrange, Histogram, normalize, sturges, StatsBase
2424

2525
import Accessors
26+
import DataAPI
2627
import GLM, Loess
2728
import FileIO
2829
import RelocatableFolders

src/algebra/select.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ select(data, d::DimsSelector) = (d,) => identity => "" => nothing
4949

5050
function select(data::Columns, name::Union{Symbol, AbstractString})
5151
v = getcolumn(data.columns, Symbol(name))
52-
return (v,) => identity => to_string(name) => nothing
52+
supports_colmetadata = DataAPI.colmetadatasupport(typeof(data.columns)).read
53+
label = if supports_colmetadata && "label" in DataAPI.colmetadatakeys(data.columns, name)
54+
to_string(DataAPI.colmetadata(data.columns, name, "label"))
55+
else
56+
to_string(name)
57+
end
58+
return (v,) => identity => label => nothing
5359
end
5460

5561
function select(data::Columns, idx::Integer)

test/algebra.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@ end
5656
@test_throws_message "not allowed to use arrays that are not one-dimensional" AlgebraOfGraphics.process_mappings(layer)
5757
end
5858

59+
@testset "column labels processing" begin
60+
df = DataFrames.DataFrame(
61+
t = 1:10,
62+
v = sin.(1:10),
63+
c = sqrt.(1:10),
64+
)
65+
66+
layer = data(df) * mapping(:t, :v, color = :c) * visual(Scatter)
67+
68+
processedlayer = AlgebraOfGraphics.process_mappings(layer)
69+
@test processedlayer.labels[1] == fill("t")
70+
@test processedlayer.labels[2] == fill("v")
71+
@test processedlayer.labels[:color] == fill("c")
72+
73+
DataFrames.colmetadata!(df, :t, "label", "Time")
74+
DataFrames.colmetadata!(df, :v, "label", "Volume")
75+
DataFrames.colmetadata!(df, :c, "label", "Concentration")
76+
77+
processedlayer = AlgebraOfGraphics.process_mappings(layer)
78+
@test processedlayer.labels[1] == fill("Time")
79+
@test processedlayer.labels[2] == fill("Volume")
80+
@test processedlayer.labels[:color] == fill("Concentration")
81+
82+
layer = data(df) * mapping(:t => "T", :v => "V", color = :c => "C") * visual(Scatter)
83+
84+
processedlayer = AlgebraOfGraphics.process_mappings(layer)
85+
@test processedlayer.labels[1] == fill("T")
86+
@test processedlayer.labels[2] == fill("V")
87+
@test processedlayer.labels[:color] == fill("C")
88+
end
89+
5990
@testset "plain `mapping`" begin
6091
layer = mapping(1:3, 4:6, text = fill("hello", 3) => verbatim)
6192
processedlayer = AlgebraOfGraphics.process_mappings(layer)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Unitful
3131
import DynamicQuantities
3232
const U = Unitful
3333
const D = DynamicQuantities
34+
import DataFrames
3435

3536
Random.seed!(1234)
3637

0 commit comments

Comments
 (0)