• Stars
    star
    176
  • Rank 216,987 (Top 5 %)
  • Language
    Nim
  • License
    MIT License
  • Created over 5 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A port of ggplot2 for Nim

ggplotnim - ggplot2 in Nim

https://github.com/Vindaar/ggplotnim/workflows/ggplotnim%20CI/badge.svg https://img.shields.io/static/v1?message=join%20chat&color=blue&label=nim-science&logo=matrix&logoColor=gold&style=flat-square&.svg https://img.shields.io/discord/371759389889003530?color=blue&label=nim-science&logo=discord&logoColor=gold&style=flat-square&.svg

This package, as the name suggests, will become a “sort of” port of ggplot2 for Nim.

It is based on the ginger package.

If you’re unfamiliar with the Grammar of Graphics to create plots, one of the best resources is probably Hadley Wickham’s book on ggplot2, for which also an online version exists at: https://ggplot2-book.org/

In general this library tries (and will continue to do so) to stay mostly compliant with the ggplot2 syntax. So searching for a solution in ggplot2 should hopefully be applicable to this (unless the feature isn’t implemented yet of course).

Note on version v0.4.0

The dataframe implementation that was part of this library until version v0.4.0 is now in its own repository under the name of Datamancer:

https://github.com/SciNim/Datamancer

This library imports and exports Datamancer, as such you don’t need to change any code, even if you only imported ggplotnim to access the dataframe.

Recipes

For a more nimish approach, check out the recipes, which should give you examples for typical use cases and things I encountered and the solutions I found. Please feel free to add examples to this file to help other people!

Note that all recipes shown there are part of the test suite. So it’s guaranteed that the plots shown there for a given version actually produce the shown result!

Documentation

The documentation is found at:

https://vindaar.github.io/ggplotnim

Installation & dependencies

Installation should be just a

nimble install ggplotnim

away. Maybe consider installing the #head, since new version probably won’t be released after every change, due to rapid development still ongoing.

Since this library is written from scratch there is only a single external dependency, which is cairo.

Windows

Using ggplotnim on Windows is made slightly more problematic, because of the default cairo backend. Installing cairo on Windows is not as straightforward as on Linux or OSX.

There are multiple options, from most complicated to easiest:

Personally I would recommend the last option. Note however that the standalone DLL is called cairo.dll, but ggplotnim expects the name libcairo-2.dll. I would recommend to put the DLL in some sane place and adding that location to your Windows PATH variable:

Simple text only instructions on how to do that:

  • Win key
  • search for “path”
  • click on “edit system environment variables”
  • click on “Environment Variables” in the bottom right corner
  • under “System variables” select “PATH” and click edit
  • click “New” and add the full path to your installation location of choice that contains the now called libcairo-2.dll

After saving those changes and restarting PowerShell / the command prompt everything should work.

Currently working features

Geoms:

  • geom_point
  • geom_line
  • geom_histogram
  • geom_freqpoly
  • geom_bar
  • geom_errorbar
  • geom_linerange
  • geom_tile
  • geom_raster
  • geom_text
  • geom_ridgeline
  • soon:
    • geom_density

Facets:

  • facet_wrap

Scales:

  • size (both for discrete and continuous data)
  • color (both for discrete and continuous data)
  • shape (multiple shapes for lines and points)

Examples

Consider looking at the recipes in addition to the below to get a fuller picture!

The following is a short example from the recipe section that shows multiple features:

  • parsing CSV files to a DF
  • performing DF operations using formulas (f{} syntax)
  • general ggplot functionality
  • composing multiple geoms to annotate specific datapoints
import ggplotnim 
let df = toDf(readCsv("data/mpg.csv"))
let dfMax = df.mutate(f{"mpgMean" ~ (`cty` + `hwy`) / 2.0})
  .arrange("mpgMean")
  .tail(1)
ggplot(df, aes("hwy", "displ")) + 
  geom_point(aes(color = "cty")) + # set point specific color mapping
  # Add the annotation for the car model below the point
  geom_text(data = dfMax,
            aes = aes(y = f{c"displ" - 0.2}, 
                      text = "model")) +
  # and add another annotation of the mean mpg above the point
  geom_text(data = dfMax,
            aes = aes(y = f{c"displ" + 0.2}, 
                      text = "mpgMean")) +
  theme_opaque() +
  ggsave("media/recipes/rAnnotateMaxValues.png")

./media/recipes/rAnnotateMaxValues.png

Experimental Vega-Lite backend

From the beginning one of my goals for this library was to provide not only a Cairo backend, but also to support Vega-Lite (or possibly Vega) as a backend. To share plots and data online (and possibly add support for interactive features) is much easier in such a way.

An experimental version is implemented in ggplot_vega.nim, which provides most functionality of the native backend, with the exception of support for facetted plots.

See the full example in the recipe here.

Creating a vega plot is done by also importing the ggplot_vega submodule and then just replacing a ggsave call by a ggvega call:

import ggplotnim
import ggplotnim/ggplot_vega
let mpg = toDf(readCsv("data/mpg.csv"))
ggplot(mpg, aes(x = "displ", y = "cty", color = "class")) +
  geom_point() +
  ggtitle("ggplotnim in Vega-Lite!") +
  ggvega("media/recipes/rSimpleVegaLite.html") # w/o arg creates a `/tmp/vega_lite_plot.html`

This recipe gives us the following plot:

media/recipes/rSimpleVegaLite.png

To view it as an interactive plot in the Vega viewer, click here.

More Repositories

1

shell

A Nim mini DSL to execute shell commands
Nim
153
star
2

NeuralNetworkLiveDemo

Contains a simple live demo of MNIST training and classification using Nim + Arraymancer + Plotly
Nim
43
star
3

ginger

A Nim package similar to R's Grid package
Nim
29
star
4

nimhdf5

Wrapper and some simple high-level bindings for the HDF5 library for the Nim language
Nim
28
star
5

LatexDSL

A mini DSL to generate LaTeX from Nim
Nim
26
star
6

TimepixAnalysis

Contains code related to calibration and analysis of Timepix based detector + CAST related code
Nim
20
star
7

llama2nim

A Nim port of Andrej Karpathy's llama2.c
Nim
20
star
8

nim-mpfit

A wrapper for the cMPFIT library for the Nim programming language, https://vindaar.github.io/nim-mpfit/
C
17
star
9

nimnlopt

A wrapper for the nonlinear optimization library Nlopt
Nim
15
star
10

rayTracingInOneWeekend

Nim
7
star
11

JsonSchemaValidator

Just a JSON Schema validator written in Nim
Nim
6
star
12

hotpatching

A hacky way to hotpatch functions at runtime!
Nim
6
star
13

forked

Syntax sugar for `cligen`'s procpool for easy multiprocessing
Nim
6
star
14

MetaPlot

An experimental plotting playground for JSON based visualization libraries
Nim
5
star
15

texParser

A basic LaTeX parser
Nim
5
star
16

flatBuffers

Another library for reading/writing Nim objects to binary files
Nim
5
star
17

llm.nim

A port of Karpathy's llm.c (CPU) to Nim
Nim
5
star
18

phd

The git repository of my PhD thesis
TeX
4
star
19

cacheMe

A basic procedure caching macro helper
Nim
4
star
20

sorted_seq

A trivial `seq[T]` type that keeps data in sorted order
Nim
4
star
21

fsmonitor2

fsmonitor but using asyncdispatch
Nim
4
star
22

TrAXer

An (Axion) X-ray raytracer for X-ray optic simulations
Nim
3
star
23

AnsysDiscoveryToLisConvert

A mundane project needs a mundane name
Nim
3
star
24

gsl-nim

A nimterop based wrapper for the GNU Scientific Library
Nim
3
star
25

NimJit

Nim
3
star
26

brokenRepl

Nim
2
star
27

SDSScompspec_C

SDSScompspec in C
C
2
star
28

AdventOfCode2017

Solutions to AdventOfCode 2017
Nim
2
star
29

orgtables

A library to turn Nim data into Org mode tables
Nim
2
star
30

mhbDataToToml

Nim
1
star
31

expressionEval

Header only math expression library for C++. Not pretty, but works.
C++
1
star
32

Py_SDSScompspec

SDSSCompspec in Python!
Python
1
star
33

MHB_test

Nim
1
star
34

TPAresources

A simple repo to store data files deemed too large for TimepixAnalaysis needed to run tests
Nim
1
star
35

libgccjit

A wrapper for libgccjit
Nim
1
star
36

AdventOfCode2020

Nim
1
star
37

phdCompanion

Companion website to my PhD thesis
CSS
1
star