# Way to make contour plot from 3 vectors?

**URL:** <https://discourse.julialang.org/t/way-to-make-contour-plot-from-3-vectors/20118>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 26, 2019, 5:53am UTC](https://discourse.julialang.org/t/way-to-make-contour-plot-from-3-vectors/20118 "2019-01-26T05:53:44Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [January 27, 2019, 1:32am UTC](https://discourse.julialang.org/t/way-to-make-contour-plot-from-3-vectors/20118/4 "2019-01-27T01:32:21Z")

</div>

Here is a Julia 1.x update for a response surface model:

```julia
import Cairo
using DataFrames, Gadfly, RDatasets

module t
using DataFrames, Statistics

VecOrSA = Union{Vector, SubArray}
expand_grid(xv::AbstractRange, yv::AbstractRange) = 
    vcat([[x y] for x in xv, y in yv]...)

function rsm(xv::VecOrSA, yv::VecOrSA, zv::VecOrSA, k::Int; 
        xlim::Tuple=extrema(xv), ylim::Tuple=extrema(yv))
    # k is the order of the polynomial surface
    y = Vector{Float64}(zv)
    μx, μy = mean(xv), mean(yv)
    p = reshape([1:k;],1,:)
    X = hcat(ones(length(xv)), (xv.-μx).^p, (yv.-μy).^p)    
    B = X'X \ X'y
    xp = range(xlim[1], xlim[2], length=11)
    yp = range(ylim[1], ylim[2], length=11)
    g = expand_grid(xp, yp)
    X = hcat(ones(size(g,1)), (g[:,1].-μx).^p, (g[:,2].-μy).^p)
    return DataFrame(x=g[:,1], y=g[:,2], z=X*B)
end

end

Doz = dropmissing(dataset("datasets","airquality"), disallowmissing=true)
Doz[:SolarRad] = Doz[:,2] .> 200
labelD = Dict(false=>"SolarRad ≤200", true=>"SolarRad >200")

Dpredict = by(Doz, :SolarRad, [:Wind, :Temp, :Ozone] =>
    x->t.rsm(x.Wind, x.Temp, x.Ozone, 2, xlim=(0, 21), ylim=(50,100)) )

set_default_plot_size(16cm, 8cm)
p = plot(Doz, x=:Wind, y=:Temp, xgroup=:SolarRad, color=:Ozone, 
    Geom.subplot_grid(
        layer( Geom.point),
        layer(Dpredict, x=:x, y=:y, z=:z, xgroup=:SolarRad, Geom.contour),
        Guide.xticks(ticks=[0:5:20;]) ),
    Scale.color_continuous(minvalue=0, maxvalue=200),
    Scale.xgroup(labels=x->labelD[x], levels=[false, true])
    )

```

![rsm](https://global.discourse-cdn.com/julialang/original/3X/1/b/1bc76ad63728a683296445b23496b2dedbe29d1e.png)

---

_[View the full topic](https://discourse.julialang.org/t/way-to-make-contour-plot-from-3-vectors/20118)._
