Skip to content

Commit

Permalink
Merge pull request #22 from iuliadmtru/svyplot
Browse files Browse the repository at this point in the history
Implement `svyplot` function with documentation and test
  • Loading branch information
ayushpatnaikgit authored Jul 30, 2022
2 parents 8682bee + 4716142 commit 64143a7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
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

0 comments on commit 64143a7

Please sign in to comment.