Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement svyplot function with documentation and test #22

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/src/assets/scatter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/Survey.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions src/svyplot.jl
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ end

include("svyglm.jl")
include("svyhist.jl")
include("svyplot.jl")
10 changes: 10 additions & 0 deletions test/svyplot.jl
Original file line number Diff line number Diff line change
@@ -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