# Smoothing over date axis in AlgebraOfGraphics

**URL:** <https://discourse.julialang.org/t/smoothing-over-date-axis-in-algebraofgraphics/72152>\
**Category:** Visualization\
**Tags:** algebraofgraphics\
**Created:** [November 27, 2021, 6:49am UTC](https://discourse.julialang.org/t/smoothing-over-date-axis-in-algebraofgraphics/72152 "2021-11-27T06:49:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sleak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleak/32/15447_2.png) [@sleak](https://discourse.julialang.org/u/sleak)\
**Post date:** [November 27, 2021, 6:49am UTC](https://discourse.julialang.org/t/smoothing-over-date-axis-in-algebraofgraphics/72152/1 "2021-11-27T06:49:37Z")

</div>

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:

```julia
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

```

 ![points](https://global.discourse-cdn.com/julialang/original/3X/f/7/f718e055a62dda2af23b1da4eba8501c3c072b03.png)

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:

```julia
days(d) = Dates.value(d - dates[1]) |> Float64
data(df) * mapping(:dates => days => :days, :weights) * smooth() |> draw

```

 ![smooth](https://global.discourse-cdn.com/julialang/original/3X/3/0/3023b82c87bd7c68770d0260e2050fc82a867b07.png)

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

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [November 27, 2021, 4:23pm UTC](https://discourse.julialang.org/t/smoothing-over-date-axis-in-algebraofgraphics/72152/2 "2021-11-27T16:23:30Z")

</div>

Hi! So, it is slightly tricky, but it is possible using the internal function that converts dates to floats before plotting. So, you could do:

```julia
using AlgebraOfGraphics: datetime2float, datetimeticks
# Convert all to floats beforehand
plt = data(df) * mapping(:dates => datetime2float, :weights) * (visual(Scatter) + smooth())
# Create the correct date ticks with `datetimeticks`
draw(plt, axis=(xticks=datetimeticks(string, dates[1:5:end]),))

```

What you wanted to do (have separate `x` values for different layers) is in theory possible with

```julia
plt = data(df) * (
    mapping(:dates, :weights) + # use dates here
    mapping(:dates => datetime2float, :weights) * smooth() # use floats here
)

```

but at the moment having different _types_ of axes (numeric versus dates) in different layers errors (the above won’t work), though it could probably be fixed.

EDIT: ups, yes, fixed typo above

---

<div class="post-metadata">

**Author:** ![sleak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sleak/32/15447_2.png) [@sleak](https://discourse.julialang.org/u/sleak)\
**Post date:** [November 28, 2021, 5:22am UTC](https://discourse.julialang.org/t/smoothing-over-date-axis-in-algebraofgraphics/72152/3 "2021-11-28T05:22:12Z")

</div>

that solved it, thanks! The variation that worked for me is:

```julia
using AlgebraOfGraphics
using AlgebraOfGraphics: datetime2float
using CairoMakie
ax = (xticks=datetimeticks(string, dates[1:5:end]),)
plt = data(df) * mapping(:dates => datetime2float, :weights) * (visual(Scatter) + smooth())
draw(plt; axis=ax)

```
