# A preview of \`StatisticalGraphics\` a new package for data visualisation in Julia

**URL:** <https://discourse.julialang.org/t/a-preview-of-statisticalgraphics-a-new-package-for-data-visualisation-in-julia/89547>\
**Category:** Visualization\
**Tags:** plotting, data, visualization, inmemorydatasets\
**Created:** [October 31, 2022, 8:46am UTC](https://discourse.julialang.org/t/a-preview-of-statisticalgraphics-a-new-package-for-data-visualisation-in-julia/89547 "2022-10-31T08:46:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sl-solution](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sl-solution/32/34759_2.png) [@sl-solution](https://discourse.julialang.org/u/sl-solution)\
**Post date:** [October 31, 2022, 8:46am UTC](https://discourse.julialang.org/t/a-preview-of-statisticalgraphics-a-new-package-for-data-visualisation-in-julia/89547/1 "2022-10-31T08:46:26Z")

</div>

I am very excited to share a preview of [`StatisticalGraphics`](https://github.com/sl-solution/StatisticalGraphics.jl), a package that I have developed for statistical data visualisation in `Julia`. The idea of the package is to develop a simple to use and yet powerful tool for statistical graphical presentation that allows users to customise every bit of the output.

The main function exported by the package is `sgplot`. Users should pass an `AbstractDataset` (see [`InMemoryDatasets.jl`](https://github.com/sl-solution/InMemoryDatasets.jl)) as the first positional argument of `sgplot` and a vector of plot types (like `Scatter`, `Line`, `Bar`, …) as the second positional argument of it. The `sgplot` function produces the requested plots based on the values in the passed data set and overlays all of the produced plots on a single graph.

Interestingly, when users pass a grouped data set (created by the `groupby`, `groupby!`, or `gatherby` functions - see [Group observations](https://sl-solution.github.io/InMemoryDatasets.jl/stable/man/grouping/)), `sgplot` produces a separate graph for each group of observations and put each graph in a separate panel.

The following examples illustrate the basic ideas of the package;

- Load packages and generate a sample data set

```julia
using StatisticalGraphics
using InMemoryDatasets

ds = Dataset(x=1:100, y=rand(100), group=rand(["g1", "g2"], 100));

```

- Create a line plot

```julia
sgplot(ds, Line(x=:x, y=:y))

```

![graph1](https://raw.githubusercontent.com/sl-solution/StatisticalGraphics.jl/main/other/graph1.svg)

- More than one plot type can be passed as the second positional argument,

```julia
sgplot(ds, [
              Line(x=:x, y=:y),
              Scatter(x=:x, y=:y)
           ])

```

![graph2](https://raw.githubusercontent.com/sl-solution/StatisticalGraphics.jl/main/other/graph2.svg)

- The plots in `StatisticalGraphics` usually support the `group` keyword argument to create the same plot for each group of observation - the final graph is an overlay plot and the groups are distinguished by colour,

```julia
sgplot(ds, [
              Line(x=:x, y=:y, group=:group),
              Scatter(x=:x, y=:y, group=:group)
           ])

```

![graph3](https://raw.githubusercontent.com/sl-solution/StatisticalGraphics.jl/main/other/graph3.svg)

- To produce a panel of graphs, user can pass a grouped data set as the first positional argument,

```julia
sgplot(groupby(ds, :group), [
              Line(x=:x, y=:y, group=:group),
              Scatter(x=:x, y=:y, group=:group)
           ])

```

![graph4](https://raw.githubusercontent.com/sl-solution/StatisticalGraphics.jl/main/other/graph4.svg)

**Bar Chart Examples**

The following examples demonstrate customised `Bar` chart with non-English axes info.

```julia
fun_example = Dataset(rand(1:5, 1000, 4), :auto)

sgplot(
        groupby(fun_example, [:x3, :x4]),
        Bar(x=:x1, group=:x2, grouporder=:data, barwidth=:nest, barcorner=15),
        nominal=:x2,
        xaxis=Axis(order=:ascending, titlecolor=:steelblue, values=([1,2,3,4,5], ["一","二","三","四","五"])),
        yaxis=Axis(title = "频率", titlecolor=:darkred),
        layout=:lattice,
        columnspace=5,
        rowspace=5,
        headercolor=:darkgreen,
        wallcolor=:lightgray,
        height=100,
        width=100,
        font="Euclid", # graph default font
        italic=true
    )

```

 ![barchart](https://global.discourse-cdn.com/julialang/original/3X/3/c/3c06670bf7ba134f983647fd1ca77b678779ab95.png)

_right to left_ characters,

 ![righ2left](https://global.discourse-cdn.com/julialang/original/3X/d/d/ddbabb5c7f174979e874b889128ebe5672795554.png)

The package is registered, however, there is no official release yet and therefore users should clone the package to access the latest features. Probably, the following items should be resolved before the first release;

- The package should work without internet connection
- Currently, parsing `DateTime` type is not consistent
- `Bar` chart must allow bar labelling
- A few plot types are missing from the package and should be added, e.g. `Text`, …
- Documentation and user guides are missing
- There is no test
