Giggly

A grammar of graphics for Elixir, after R's ggplot2: a plot maps a table's columns to aesthetics and is built up from layers (each a geom, a stat and a position), scales, a facet and a coordinate system, then drawn as SVG, for Phoenix LiveView, Livebook or a file. There is no JavaScript to run: the SVG is the plot.

svg =
  GigDatasets.mtcars()
  |> Giggly.Plot.new(%{x: :wt, y: :mpg, color: :cyl})
  |> Giggly.Plot.geom_point()
  |> Giggly.Plot.labs(x: "Weight (1000 lb)", y: "Miles per gallon")
  |> Giggly.to_svg()

File.write!("mtcars.svg", svg)

A plot is a value: Giggly.Plot describes it and runs nothing, and the functions in Giggly run it, to_svg/2 to draw it and layer_data/2 to show the table a layer computes. If you know ggplot2, the names will be familiar: geom_point(), scale_color_viridis_c(), facet_wrap(), coord_radial() and the rest are functions of Giggly.Plot.

Installation

def deps do
  [{:giggly, "~> 0.2"}]
end

Text is measured with a table of Liberation Sans's glyphs, as ggplot2's own test drawings are. To measure and draw text in the fonts you name, add Pica too; the Fonts guide says how.

A plot's data is any table Table.Reader reads: a list of maps, a map of columns, a database result, an Explorer data frame. With Explorer in your dependencies too, a data frame, lazy or eager, is kept in Explorer as the plot reads it, each column of the type Explorer declares, and a layer computes there up to its stat wherever its facet, stat, mapping and scales allow, so only the stat's small output comes into Elixir; the Giggly.Plot documentation's Data section says which layers do.

The tables in the examples, R's and ggplot2's such as mtcars and diamonds, are a package of their own, gig_datasets, as plain data.

Documentation

The Giggly.Plot documentation describes a plot and every layer, scale, facet, coordinate system and theme function; the Giggly documentation, running one. Guides:

Notebooks

The repository's notebooks/ holds a Livebook notebook for every page of ggplot2's function reference, each following its page example by example and drawing each one Giggly can draw so far. Start with notebooks/index.livemd, which follows ggplot2's reference index and links every notebook. Each notebook installs Giggly, and kino_giggly, which draws its plots, and gig_datasets, which holds its tables, from Hex, so it runs wherever it is opened from, drawing with the released Giggly.

License

MIT; see LICENSE. The datasets and glyph tables Giggly copies from R packages are listed with their licenses and sources in THIRD_PARTY_NOTICES.md.