14 Ranking
Bar charts and their variants β comparing quantities across categories.
(ns plotje-book.ranking
(:require
;; Kindly -- notebook rendering protocol
[scicloj.kindly.v4.kind :as kind]
;; Rdatasets -- standard datasets
[scicloj.metamorph.ml.rdatasets :as rdatasets]
;; Plotje -- composable plotting
[scicloj.plotje.api :as pj]))(def sales {:product [:widget :gadget :gizmo :doohickey]
:revenue [120 340 210 95]})Bar Chart
Count occurrences of a categorical column.
(-> (rdatasets/datasets-iris)
(pj/lay-bar :species))Colored Bar Chart
Grouped (dodged) bars β count by day, colored by smoking status.
(-> (rdatasets/reshape2-tips)
(pj/lay-bar :day {:color :smoker}))Stacked Bar Chart
Same data, stacked instead of dodged.
(-> (rdatasets/reshape2-tips)
(pj/lay-bar :day {:position :stack :color :smoker}))Stacked Bar (Proportions)
100% stacked bars β shows proportions instead of counts.
(-> (rdatasets/palmerpenguins-penguins)
(pj/lay-bar :island {:position :fill :color :species}))Horizontal Bar Chart
Flip the bar chart for horizontal orientation.
(-> (rdatasets/datasets-iris)
(pj/lay-bar :species)
(pj/coord :flip))(pj/coord :flip) draws categories bottom-to-top in data order, matching ggplot2βs coord_flip(). For a ranking chart where the biggest value should appear at the top, sort the data ascending before plotting, e.g. (tc/order-by data [:value] [:asc]).
Horizontal Colored Bars
Colored bars, flipped.
(-> (rdatasets/reshape2-tips)
(pj/lay-bar :day {:color :time})
(pj/coord :flip))Value Bar
Pre-computed y values (no counting).
(-> sales
(pj/lay-value-bar :product :revenue))Value Bar (Horizontal)
Flip for horizontal orientation.
(-> sales
(pj/lay-value-bar :product :revenue)
(pj/coord :flip))Lollipop
Stem + dot β a lighter alternative to bar charts.
(-> sales
(pj/lay-lollipop :product :revenue))Lollipop (Horizontal)
Flipped for horizontal orientation.
(-> sales
(pj/lay-lollipop :product :revenue)
(pj/coord :flip))Whatβs Next
- Change Over Time β line charts, step functions, and stacked areas
- Configuration β control dimensions, palettes, and themes