No description
Find a file
2025-04-16 20:13:26 +02:00
.github/workflows [CI] fix push events to only run when pushing to master branch 2024-10-26 11:41:49 +02:00
benchmarks remove datamancer benchmark 2021-06-17 16:40:55 +02:00
bookPlots fix bookPlots/chapter2 code to work with Option settings 2020-03-09 18:17:52 +01:00
data [recipes] add recipe for geom_smooth 2021-07-27 17:37:39 +02:00
docs [docs] fix path to dochack in doc generation tool 2021-12-16 13:17:11 +01:00
issues move images for understandFastSlowWeird.org and update links in Org 2019-11-16 09:48:20 +01:00
media update some plots, recipes to fix the CI 2024-09-05 11:21:59 +02:00
playground remove dataframe related files from playground 2021-06-17 16:40:55 +02:00
recipes [recipes] update col reference syntax in Newton acc recipe 2024-05-06 12:07:04 +02:00
resources/expected update some plots, recipes to fix the CI 2024-09-05 11:21:59 +02:00
src fix issue with drawing transformed error bars and other plots 2025-04-16 20:11:13 +02:00
tests [tests] update test comparison threshold 2024-02-11 16:22:17 +01:00
.gitignore generate all JSON files in one compilation 2020-12-31 00:11:13 +01:00
.travis.yml take out rFacetRaster from img cmp, print nim --version in travis 2020-07-10 11:36:40 +02:00
changelog.org update changelog, push v0.7.4 2025-04-16 20:13:26 +02:00
ggplotnim.nimble update changelog, push v0.7.4 2025-04-16 20:13:26 +02:00
LICENSE add license and a first README 2019-05-25 00:39:52 +02:00
nim.cfg [cfg] enable Cairo and TikZ backends by default 2023-09-12 16:56:22 +02:00
README.org [README] finalize badges 2021-11-27 16:06:29 +01:00
recipes.org [recipes] update col reference syntax in Newton acc recipe 2024-05-06 12:07:04 +02:00

ggplotnim - ggplot2 in Nim

https://github.com/Vindaar/ggplotnim/workflows/ggplotnim%20CI/badge.svg https://img.shields.io/static/v1?message=join 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")

/rex/ggplotnim/media/branch/master/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:

/rex/ggplotnim/media/branch/master/media/recipes/rSimpleVegaLite.png

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