I have a bunch of observations (date on x-axis, weight on y-axis) that I want to plot a trend line through. Using DataFrames and AlgebraOfGraphics I can plot the individual points:
using AlgebraOfGraphics
using CairoMakie
using DataFrames
using Dates
dates = as_date.(["15/05", "18/05", "25/05", "31/05", "09/06",
"15/06", "24/06", "07/07", "15/07", "01/08", "18/08",
"01/09", "20/09", "28/09", "05/10", "17/10", "08/11", "17/11", "23/11"])
weights = [21.8, 22.5, 21.4, 21.8, 22.2, 22.6, 21.8, 22.8, 23.4, 22.9, 22.9, 22.7, 21.8, 22.3, 22.2, 22.0, 21.7, 21.7, 20.8]
df = DataFrame(dates=dates, weights=weights)
data(df) * mapping(:dates, :weights) |> draw
I can’t apply smoothing directly - because the loess()
function in smooth()
needs both x
and y
to be convertible to Float64
and my x axis is dates, but I can convert the a-axis to “days since start” and apply smoothing to that:
days(d) = Dates.value(d - dates[1]) |> Float64
data(df) * mapping(:dates => days => :days, :weights) * smooth() |> draw
How do I overlay the smooth line over the points plot, so that the created plot has dates on the x-axis? (or to put it another way, add two layers that have different mappings for the x axis)
thanks,
Steve