31 ggplot2 Equivalents
A reader who knows ggplot2 arrives with a plot already in mind and a function name for each part of it. The question is what that function is called in Plotje, and the answers are taught across several chapters, each in the context of its own topic. This chapter collects them: each row gives a ggplot2 spelling, the Plotje spelling, and the section of the book that teaches it. Rows with no Plotje spelling are listed at the end.
The examples use the iris and mpg datasets from the RDatasets collection.
(ns plotje-book.ggplot2-equivalents
(:require
;; Rdatasets -- standard datasets
[scicloj.metamorph.ml.rdatasets :as rdatasets]
;; Tablecloth -- dataset manipulation
[tablecloth.api :as tc]
;; Kindly -- notebook rendering protocol
[scicloj.kindly.v4.kind :as kind]
;; Plotje -- composable plotting
[scicloj.plotje.api :as pj]))Mappings and layers
| ggplot2 | Plotje | Taught in |
|---|---|---|
ggplot(df, aes(x = a, y = b)) |
(pj/pose df :a :b), or (pj/pose df {:x :a :y :b}) |
Core Concepts |
+ geom_point() |
pj/lay-point |
Core Concepts |
+ geom_smooth(method = "lm") |
(pj/lay-smooth {:stat :linear-model :confidence-band true}); without :confidence-band the line is drawn alone, as se = FALSE draws it |
Relationships |
+ geom_point(data = df2) |
(pj/lay-point {:data df2}) |
Core Concepts |
+ geom_point(aes(x = c, y = d)), a layer on other columns |
(pj/lay-point :c :d), drawn on the same panel, each axis titled by both columns |
Panels |
pivot_longer() then aes(color = name) |
(pj/lay-line df :year [:north :south]), a series |
Series |
Where the layers of a panel draw different columns and no layer is colored by a column, Plotje gives each layer a color and a legend entry naming its columns; ggplot2 draws both layers in one color with no legend.
ggplot2βs geom_smooth() draws a 95% confidence ribbon around the line unless se = FALSE is given. Plotje draws the ribbon when :confidence-band true is given, at the same 95% level. A pose with a point layer and a linear-model smooth with its ribbon, the Plotje spelling of ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + geom_smooth(method = "lm"):
(-> (rdatasets/datasets-iris)
(pj/pose :sepal-length :sepal-width)
pj/lay-point
(pj/lay-smooth {:stat :linear-model :confidence-band true}))Under a facet, ggplot2 divides a layerβs data = among the panels by the facet column, as it divides the plotβs data. Plotje divides the poseβs data only: a layer that carries :data of its own is drawn whole in every panel. Here the regression fitted to the setosa rows is drawn in each of the three species panels, where ggplot2 4.0.0 draws it in the setosa panel alone:
(-> (rdatasets/datasets-iris)
(pj/pose :sepal-length :sepal-width)
pj/lay-point
(pj/lay-smooth {:stat :linear-model
:data (tc/select-rows (rdatasets/datasets-iris)
#(= "setosa" (:species %)))})
(pj/facet :species))For layer data that differs from panel to panel, build the panels with pj/arrange, as Known Limitations describes.
Layer types and positions
| ggplot2 | Plotje | Taught in |
|---|---|---|
aes(colour = species) in ggplot() |
(pj/pose df {:color :species}) |
Core Concepts |
aes(colour = species) in a geom |
(pj/lay-point {:color :species}) |
Core Concepts |
geom_point(colour = "red") |
(pj/lay-point {:color "red"}); where the data has a column named "red", that column is mapped instead |
Core Concepts |
geom_line() + geom_point() |
(-> df (pj/pose :year :sales) pj/lay-line pj/lay-point) |
Core Concepts |
geom_bar(), bars of counts |
(pj/lay-bar df :class) |
Ranking |
geom_col(), bars of values |
(pj/lay-bar df :product :revenue), with one row per bar |
Ranking |
geom_bar(aes(fill = drv)), stacked |
(pj/lay-bar df :class {:color :drv :position :stack}); without :position the bars are dodged |
Ranking |
geom_histogram() |
pj/lay-histogram |
Distributions |
geom_boxplot() |
pj/lay-boxplot |
Distributions |
geom_violin() |
pj/lay-violin |
Distributions |
geom_errorbar(aes(ymin = lo, ymax = hi)) |
(pj/lay-errorbar {:y-min :lo :y-max :hi}) |
Cookbook |
stat_summary(fun.data = mean_se) |
pj/lay-summary |
Cookbook |
geom_hline(yintercept = 3) |
(pj/lay-rule-h {:y-intercept 3}) |
Cookbook |
geom_text(aes(label = name)) |
(pj/lay-text {:text :name}) |
Cookbook |
geom_tile(aes(fill = z)) |
(pj/lay-tile df :a :b {:fill :z}) |
Relationships |
ggridges::geom_density_ridges() |
(pj/lay-ridgeline df :species :sepal-length), the category column first β the reverse of ggridgesβ aes(x = value, y = group) |
Distributions |
ggplot2βs geom_bar() stacks the bars of a filled group; Plotjeβs pj/lay-bar dodges them, side by side, unless :position :stack is given. The same counts of mpgβs classes by drive, dodged and then stacked:
(-> (rdatasets/ggplot2-mpg)
(pj/lay-bar :class {:color :drv}))(-> (rdatasets/ggplot2-mpg)
(pj/lay-bar :class {:color :drv :position :stack}))Scales
| ggplot2 | Plotje | Taught in |
|---|---|---|
+ scale_x_log10() |
(pj/scale :x :log) |
Core Concepts, Scales |
+ scale_x_reverse() |
(pj/scale :x {:domain [high low]}), a :domain written high to low; the axis ends at the two values written, where ggplot2 reverses the dataβs own extent |
Scales |
+ expand_limits(y = 0) |
(pj/scale :y {:include 0}) |
Scales |
+ scale_color_manual(values = ...) |
(pj/scale :color {:values [...]}) |
Scales |
A log axis and a reversed axis on one plot β the population on a log scale, the area written from high to low:
(-> {:population [1000 5000 50000 200000 1000000 5000000]
:area [2 8 30 120 500 2100]}
(pj/lay-point :population :area)
(pj/scale :x :log)
(pj/scale :y {:domain [2500 0]})):include makes an axis reach a value the data does not:
(-> (rdatasets/datasets-iris)
(pj/lay-point :sepal-length :sepal-width)
(pj/scale :y {:include 0}))Coordinates
| ggplot2 | Plotje | Taught in |
|---|---|---|
+ coord_flip() |
(pj/coord :flip) |
Core Concepts |
+ coord_fixed() |
(pj/coord :fixed) |
Core Concepts |
+ coord_polar() |
(pj/coord :polar) |
Polar |
+ coord_cartesian(ylim = c(3, 3.5), expand = FALSE) |
(pj/scale :y {:domain [3 3.5]}); the panel ends at the two values written, with no padding |
Scales |
ggplot2 has two ways to narrow an axis. coord_cartesian(ylim =) narrows the view and keeps every row, while the scale limits scale_y_continuous(limits =) drop the rows outside them β measured with ggplot2 4.0.0 on iris narrowed to sepal widths from 3 to 3.5, coord_cartesian keeps all 150 rows and the scale limits keep 74. A :domain on an axis behaves as coord_cartesian does, though it is written on the scale. All 150 rows are drawn and clipped at the panel edge:
(-> (rdatasets/datasets-iris)
(pj/lay-point :sepal-length :sepal-width)
(pj/scale :y {:domain [3 3.5]}))Facets and arrangements
| ggplot2 | Plotje | Taught in |
|---|---|---|
+ facet_wrap(~ drv) |
(pj/facet pose :drv), which draws its panels in one row |
Faceting |
+ facet_wrap(~ class, ncol = 4) |
(pj/facet pose :class {:cols 4}) |
Wrapping the panels into rows |
+ facet_grid(cyl ~ drv) |
(pj/facet pose {:col :drv :row :cyl}), with (pj/scale :row {:domain [4 5 6 8]}) for ggplot2βs sorted rows |
Faceting |
pivot_longer() then facet_wrap(~ name, ncol = 1, scales = "free_y") |
(pj/facet pose {:y [:hwy :cty]}), one panel per column, stacked in the order written |
A Panel per Column |
patchworkβs p1 | p2 |
(pj/arrange [p1 p2]), with one shared legend, as with plot_layout(guides = "collect") |
Composition |
GGally::ggpairs(df, columns = cols) |
(pj/matrix df cols) |
Relationships |
Facetingβs own table covers the faceting options β free scales, strip labels, margins β in more detail.
A grid of categories, the Plotje spelling of facet_grid(cyl ~ drv). Plotje draws the categories of a row or column in the order the data holds them, and mpg holds the cylinder counts as 4, 6, 8, 5. ggplot2 sorts them, so a :domain on the :row scale gives ggplot2βs order:
(-> (rdatasets/ggplot2-mpg)
(pj/lay-point :displ :hwy)
(pj/facet {:col :drv :row :cyl})
(pj/scale :row {:domain [4 5 6 8]}))Labels and options
| ggplot2 | Plotje | Taught in |
|---|---|---|
+ labs(title = ..., x = ..., y = ...) |
(pj/options {:title ... :x-label ... :y-label ...}) |
Customization |
+ labs(subtitle = ..., caption = ...) |
(pj/options {:subtitle ... :caption ...}) |
Customization |
+ labs(color = ...) |
(pj/options {:color-label ...}) |
Customization |
+ theme(legend.position = "none") |
(pj/options {:legend-position :none}) |
Customization |
ggsave("plot.png") |
(pj/save pose "plot.png") |
Cookbook |
theme_set(...) |
pj/set-config! |
Configuration |
Options and Scopes says which of Plotjeβs categories β mapping, layer, plot option, configuration β each ggplot2 construct falls in.
(-> (rdatasets/datasets-iris)
(pj/lay-point :sepal-length :sepal-width {:color :species})
(pj/options {:title "Iris Sepal Measurements"
:x-label "Length (cm)"
:y-label "Width (cm)"
:color-label "Species"}))Not available
Some ggplot2 features have no Plotje spelling in this release:
| ggplot2 | Plotje | Taught in |
|---|---|---|
after_stat(count) inside aes() |
not available; aggregate the data first and map the computed column | Known Limitations |
aes(linetype = ...) |
not available; {:stroke-dash [5 5]} on a line layer dashes the whole layer, and no column varies the dashes |
Known Limitations |
scale_x_sqrt() |
not available; the axis types are :linear, :log and :categorical |
Known Limitations |
position_jitterdodge() |
not available; {:jitter true :position :dodge} dodges the groups, but each point is jittered across more than its groupβs share of the band, so neighbouring groups overlap and points of an outer group can fall outside the panel |
|
facet_grid(scales = "free_y"), one :y per row of panels |
not available; {:share-scales #{:x}} fits :y to each panel instead |
Faceting |
A square-root axis reports an error that lists the types an axis accepts:
(try
(-> (rdatasets/datasets-iris)
(pj/lay-point :sepal-length :sepal-width)
(pj/scale :x :sqrt)
pj/plan)
(catch clojure.lang.ExceptionInfo e
(ex-message e)))"Unknown scale type: :sqrt. Supported for :x: [:categorical :linear :log]."See Also
Options and Scopes β which category each ggplot2 construct falls in.
Faceting β the faceting functions and options, one row each.
Specifying Aesthetics β
aes()inside and outside, and the identity scales.Known Limitations β every gap in the current release.