[ANN] hgg — a Haskell-native grammar of graphics

Hello everyone,

I have released hgg, a Haskell-native declarative plotting library based on the grammar of graphics.

Plots in hgg are pure values built by monoid composition:

raw |>>

 layer

   ( scatter "x" "y"

  <> colorBy "group"

   )

<> layer (statLm “x” “y”)

<> title “Result”

<> theme ThemeGrey

Current features include:

- Common statistical plots, including scatter, line, bar, histogram, density, box, violin, heatmap, contour, and forest plots

- Layered composition, facets, subplots, annotations, themes, scales, and legends

- SVG, PDF, PNG, TikZ, and IHaskell output

- DataFrame integration

- Statistical model integration with hanalyze

- 3D plots, MCMC diagnostics, DAGs

- Custom marks

The umbrella “hgg” package includes the core library, DataFrame binding, and SVG backend:

build-depends: hgg

The project is currently practical but pre-1.0, and the API is still stabilising.

GitHub:

Hackage:

Feedback on the API, documentation, portability, generated figures, and missing use cases would be very helpful. Bug reports and contributions are also welcome.

31 Likes

Neat! I don’t particularly expect to have a use for this any time soon, but I have a fondness for gg-style libraries.

Not exactly a request, but I’ve written a command-line interface to plotnine (a python gg-like) which I find useful, and it’s plausible that a command-line interface to hgg would be useful too.

1 Like

Thanks! That’s an interesting use case.

I already have a small command-line interface for hanalyze, my statistical analysis library, so rather than creating a separate CLI specifically for hgg, one possible direction would be to add hgg-based plotting to the hanalyze CLI in the future.

That could support workflows such as reading CSV data, running an analysis, and producing plots from a single command. I’ll keep your plotnine CLI in mind when considering the design.

1 Like

Hi! This looks great!

Is this flexible enough to support cowplot-style theme? As in Introduction to cowplot • cowplot

(Also I see there’s support for polar coordinates, any chance this could also have ternary triangular coordinates as with ggtern? see ggtern: Ternary Diagrams Using ggplot2 | Journal of Statistical Software )

Thanks — I checked the code rather than guessing, so here’s the honest state.

cowplot-style themes: mostly yes, and the gaps are going into the next update. Themes are a monoidal record, so you build a custom theme by chaining setters with <> and binding it to a name — reusable just like theme_cowplot(). Closest starting points today: ThemeClassic for theme_cowplot(), ThemeMinimal for theme_minimal_grid(), ThemeVoid for theme_map(). plot_grid() has a counterpart in subplots/subplotCols, which nest and inherit the outer theme.

Missing so far: separate major/minor grid toggles, tick length and direction, plot margins, legend position as a theme field, rel_widths/rel_heights, and automatic panel tags. Those are exactly what gives cowplot its look, so a faithful theme_cowplot() isn’t possible yet — but they’re all additive fields. They’re queued for the next update, and I’ll ship themeCowplot / themeMinimalGrid / themeMap along with them. If you have a priority order among them, an issue would be welcome.

Ternary: on the list, but no timeline yet. Coord is a closed ADT and polar was added to it after the fact, so ternary follows the same route. Three things come first: the layout is fixed to x and y with no third positional aesthetic; clipping and panels are rectangle-only down to the primitive layer and all four backends, so a triangular panel needs polygon clipping; and coordinate handling has leaked into individual geoms, so the projection needs consolidating before a new coordinate system carries all the marks along. None of it is a redesign, just work — and I’d rather do it properly than ship a half-version, so I can’t commit to a date.

1 Like

Oh cool thanks! I think I’ll try to contribute the cowplot style when I’m plotting stuff for whichever paper comes next.

Re ggtern, that sounds very workable. It’s not a priority for me but certainly a nice thing to have for the future.

1 Like

Nice! Is there a Cairo back-end (or a path from one of the back-ends to Cairo)?

Also the Haddock is in Japanese?

1 Like

Thanks! There is no Cairo backend or built-in path to Cairo at the moment, but I appreciate knowing that there is interest in one. I can’t promise support yet, though it is something I may consider if there is broader demand.

And yes, sorry—the Haddock is currently mostly in Japanese. hgg started as an internal tool, and I open-sourced it before finishing the English documentation. I plan to add English alongside the Japanese progressively.

3 Likes

That would be great—thank you! The theme API is still evolving, so please let me know if you find that any controls needed for a cowplot-style theme are missing.