diff --git a/docs/src/assets/scatter.png b/docs/src/assets/scatter.png new file mode 100644 index 00000000..774107e0 Binary files /dev/null and b/docs/src/assets/scatter.png differ diff --git a/src/Survey.jl b/src/Survey.jl index 82fb20c9..e617bbbf 100644 --- a/src/Survey.jl +++ b/src/Survey.jl @@ -13,12 +13,14 @@ include("svyby.jl") include("example.jl") include("svyglm.jl") include("svyhist.jl") +include("svyplot.jl") export svydesign, svyby, svyglm export data, api, apiclus1, apiclus2, apipop, apistrat, apisrs export svymean, svytotal, svyquantile export @formula export svyhist, sturges, freedman_diaconis +export svyplot export #families Normal, diff --git a/src/svyplot.jl b/src/svyplot.jl new file mode 100644 index 00000000..18a44f0d --- /dev/null +++ b/src/svyplot.jl @@ -0,0 +1,36 @@ +""" +``` + svyplot(design, x, y; weights, kwargs...) +``` +Scatter plot of survey design variables `x` and `y`. + +Weights can be specified by a `Symbol` or an `AbstractVector` passed to the +keyword argument `weights`. + +For the complete argument list see [Makie.scatter](https://makie.juliaplots.org/stable/examples/plotting_functions/scatter/index.html#scatter). + +```@example svyplot +julia> using survey + +julia> data(api); + +julia> dstrat = svydesign(data = apistrat, id = :1, strata = :stype, weights = :pw, fpc = :fpc); + +julia> s = svyplot(dstrat, :api99, :api00; weights = :pw) +``` + +![](./assets/scatter.png) +""" +function svyplot(design::svydesign, x::Symbol, y::Symbol; + weights::Union{Symbol, AbstractVector} = ones(size(design.variables, 1)), + kwargs... + ) +xs = design.variables[!, x] +ys = design.variables[!, y] + +if isa(weights, Symbol) +weights = design.variables[!, weights] +end + +scatter(xs, ys; markersize = weights, marker = '○', kwargs...) +end diff --git a/test/runtests.jl b/test/runtests.jl index d99ab7eb..0cbd9b94 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -18,3 +18,4 @@ end include("svyglm.jl") include("svyhist.jl") +include("svyplot.jl") diff --git a/test/svyplot.jl b/test/svyplot.jl new file mode 100644 index 00000000..f8b2e588 --- /dev/null +++ b/test/svyplot.jl @@ -0,0 +1,10 @@ +using Survey +using Test + +@testset "svyplot.jl" begin + data(api) + dstrat = svydesign(data = apistrat, id = :1, strata = :stype, weights = :pw, fpc = :fpc) + s = svyplot(dstrat, :api99, :api00; weights = :pw) + + @test getindex(s.plot.markersize) == dstrat.variables[!, :pw] +end