27  Polar Coordinates

For polar coordinates, (pj/coord :polar) maps x to angle and y to radius. Bars become arc-interpolated wedges (rose charts), and scatter points wrap onto a polar plane. The x domain is wrapped once round the circle, clockwise from 12 o’clock, so the two ends of the domain meet at the top. Supported marks today are :point, :bar, :rect, :text, and :rug; other marks report an error. The :x title below the panel names the column drawn as the angle, and its tick labels stand around the circle, one at each spoke of the grid. The :y title beside the panel names the column drawn as the radius, and its tick labels stand at the left edge, each level with the top of its circle of the grid. Legends render as they would on Cartesian coordinates – a :color mapping produces a normal legend that can be repositioned via :legend-position (see Customization).

(ns plotje-book.polar
  (:require
   ;; Rdatasets -- standard datasets
   [scicloj.metamorph.ml.rdatasets :as rdatasets]
   ;; Kindly -- notebook rendering protocol
   [scicloj.kindly.v4.kind :as kind]
   ;; Plotje -- composable plotting
   [scicloj.plotje.api :as pj]))
(def wind {:direction ["N" "NE" "E" "SE" "S" "SW" "W" "NW"]
           :speed [12 8 15 10 7 13 9 11]})

Polar Scatter

The same scatter plot, wrapped onto a polar plane. x maps to angle (clockwise from 12 o’clock), y maps to radius (center = minimum, edge = maximum). Iris’s :sepal-length, 4.3 to 7.9, is wrapped round the whole circle, so the three species, which differ in sepal length, sit in different directions from the center.

(-> (rdatasets/datasets-iris)
    (pj/lay-point :sepal-length :sepal-width {:color :species})
    (pj/coord :polar))
sepal widthsepal lengthspeciessetosaversicolorvirginica4.55.05.56.06.57.07.58.0234

Rose Chart (Coxcomb Diagram)

A bar chart in polar coordinates produces a rose chart. The radius axis starts at zero, so a wedge’s length from the center is in proportion to its count, and its area grows with the square of the count. Bars are arc-interpolated for smooth curved edges.

(-> (rdatasets/datasets-iris)
    (pj/lay-bar :species)
    (pj/coord :polar))
countspeciessetosaversicolorvirginica02040

Wind Rose

A bar chart with a y column (value bars) in polar makes a wind rose – each direction gets a wedge whose length is in proportion to the wind speed.

(-> wind
    (pj/lay-bar :direction :speed)
    (pj/coord :polar))
speeddirectionNNEESESSWWNW051015

Polar Stacked Bar

Stacked bars in polar show composition within each wedge. The islands are named around the circle, and the legend names the species each color stands for.

(-> (rdatasets/palmerpenguins-penguins)
    (pj/lay-bar :island {:position :stack :color :species})
    (pj/coord :polar))
countislandspeciesAdelieGentooChinstrapTorgersenBiscoeDream050100150

Polar Histogram

A histogram in polar wraps bins around the circle. Useful for circular distributions, such as time of day or compass bearing.

(-> (rdatasets/datasets-iris)
    (pj/lay-histogram :sepal-length)
    (pj/coord :polar))
countsepal length4.55.05.56.06.57.07.58.001020

Facets on Polar Coordinates

pj/facet splits a polar plot into panels as it does a Cartesian one, and each panel draws its own circle. The circle fits the shorter side of its panel’s drawing area, so three panels side by side on the default canvas are tall and narrow and draw small circles. A wider, shorter canvas gives each circle more room:

(-> (rdatasets/datasets-iris)
    (pj/lay-point :sepal-length :sepal-width {:color :species})
    (pj/coord :polar)
    (pj/facet :species)
    (pj/options {:width 900 :height 340}))
sepal widthsepal lengthspeciessetosaversicolorvirginica567823456785678setosaversicolorvirginica

Explicit Labels with Polar

The axis titles name the angle’s column and the radius’s column, as on Cartesian coordinates. pj/options replaces them, and sets a title, as it does on Cartesian coordinates:

(-> (rdatasets/datasets-iris)
    (pj/lay-point :sepal-length :sepal-width {:color :species})
    (pj/coord :polar)
    (pj/options {:title "Iris in Polar Space"
                 :x-label "Sepal length (angle)"
                 :y-label "Sepal width (radius)"}))
Iris in Polar SpaceSepal width (radius)Sepal length (angle)speciessetosaversicolorvirginica4.55.05.56.06.57.07.58.0234

See Also

What’s Next

source: notebooks/plotje_book/polar.clj