How to plot more ticks in AlgebraOfGraphics or Makie?

I have this code to plot a time-series:

using AlgebraOfGraphics

data(
    (; x=1:100, y=randn(100))
) * mapping(:x, :y) * visual(Lines) |> draw

It produces this plot:

Question: how to add more ticks to x and/or y axis without constructing them manually? For example, I’d like to have 10 ticks on the x axis, not 3.

I can’t just use axis = (xticks = LinearTicks(10),) because in the actual plot I’m working on the x axis actually contains dates, so the ticks are autogenerated by AlgebraOfGraphics.

I ran into this same issue too (`LinearTicks` seems to alter the formatting for timeseries data · Issue #232 · JuliaPlots/AlgebraOfGraphics.jl · GitHub).

I think there is work being done upstream (Implement time axis by SimonDanisch · Pull Request #1347 · JuliaPlots/Makie.jl · GitHub) so that we can handle time axes directly with Makie, but in the meantime @piever added this convenience function in AoG that I think is really handy:

julia> using AlgebraOfGraphics, GLMakie, Dates

julia> t = DateTime(2021, 1, 1):Month(1):DateTime(2022, 1, 1);

julia> y = randn(length(t));

julia> df = (; t, y);

julia> ticks = AlgebraOfGraphics.datetimeticks(monthname, df.t[begin:2:end]);

julia> plt = data(df) * mapping(:t, :y) * visual(Lines);

julia> draw(plt; axis=(; xticks=ticks))

You can pass any formatting function you want, although you will still need to play with the array of time points that is actually passed to it

2 Likes