From 64f3d589b68cb1e227f28940375045ee3dfaf481 Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Wed, 7 Apr 2021 14:39:41 -0700 Subject: [PATCH 1/3] correct scatterpath + add linearfit correct scatterpath so that color does not change (see https://github.com/JuliaPlots/Plots.jl/issues/1495). Add linearfit. --- src/recipes.jl | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/recipes.jl b/src/recipes.jl index d0fbf136..5b25936a 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -161,20 +161,51 @@ end # path and scatter # create a path from steps +# see https://github.com/JuliaPlots/Plots.jl/issues/1495 @recipe function f(::Type{Val{:scatterpath}}, x, y, z) x := x y := y seriestype := :scatter + @series begin + () + end @series begin seriestype := :path label := "" primary := false () end + primary := false () end @deps scatterpath path scatter + +# --------------------------------------------------------------------------- +# regression line and scatter + +# plots line corresponding to linear regression of y on a constant and x +@recipe function f(::Type{Val{:linearfit}}, x, y, z) + seriestype := :scatter + x := x + y := y + @series begin + () + end + X = hcat(ones(length(x)), x) + yhat = X * (X'X \ X'y) + @series begin + y := yhat + seriestype := :path + label := "" + primary := false + () + end + primary := false + () +end + + @specialize From 50adb6cd9cd2d2fccea8f579cc160bb45e4f2e7c Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Wed, 7 Apr 2021 14:42:12 -0700 Subject: [PATCH 2/3] Update recipes.jl --- src/recipes.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/recipes.jl b/src/recipes.jl index 5b25936a..c804c745 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -161,7 +161,6 @@ end # path and scatter # create a path from steps -# see https://github.com/JuliaPlots/Plots.jl/issues/1495 @recipe function f(::Type{Val{:scatterpath}}, x, y, z) x := x y := y @@ -186,9 +185,9 @@ end # plots line corresponding to linear regression of y on a constant and x @recipe function f(::Type{Val{:linearfit}}, x, y, z) - seriestype := :scatter x := x y := y + seriestype := :scatter @series begin () end From 67f4d79c4a2fc9842f051ae827be5819e07d97a1 Mon Sep 17 00:00:00 2001 From: matthieugomez Date: Thu, 8 Apr 2021 10:11:09 -0700 Subject: [PATCH 3/3] simpler formula for yhat --- src/recipes.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/recipes.jl b/src/recipes.jl index c804c745..952bf0aa 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -191,10 +191,8 @@ end @series begin () end - X = hcat(ones(length(x)), x) - yhat = X * (X'X \ X'y) @series begin - y := yhat + y := mean(y) .+ cov(x, y) / var(x) .* (x .- mean(x)) seriestype := :path label := "" primary := false