13 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]})Count bars
pj/lay-bar with only a category column counts the rows in each category β the bar height is the number of occurrences.
Plain
Count occurrences of a categorical column.
(-> (rdatasets/datasets-iris)
(pj/lay-bar :species))Colored (dodged)
Grouped (dodged) bars β count by day, colored by smoking status.
(-> (rdatasets/reshape2-tips)
(pj/lay-bar :day {:color :smoker}))Stacked
Same data, stacked instead of dodged.
(-> (rdatasets/reshape2-tips)
(pj/lay-bar :day {:position :stack :color :smoker}))Proportions (100% stacked)
100% stacked bars β shows proportions instead of counts.
(-> (rdatasets/palmerpenguins-penguins)
(pj/lay-bar :island {:position :fill :color :species}))Horizontal
Counting bars have no native horizontal form (the category would need to be counted onto the y-axis), so flip the chart instead.
(-> (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
Colored bars, flipped.
(-> (rdatasets/reshape2-tips)
(pj/lay-bar :day {:color :time})
(pj/coord :flip))Value bars
Give pj/lay-bar a value column too and it uses those numbers directly as the bar heights β no counting.
Vertical
One bar per product; the bar height is the revenue.
(-> sales
(pj/lay-bar :product :revenue))Horizontal
Put the category on y β no coord flip needed (unlike count bars).
(-> sales
(pj/lay-bar :revenue :product))Rotated labels
Horizontal bars are one way to fit long category names. To keep the bars vertical instead, rotate the x-tick labels with :x-tick-angle (see Customization for details, including :x-tick-label-pad):
(-> {:department ["Office Supplies" "Electronics" "Home Goods"
"Sporting Gear" "Garden Tools" "Toys"]
:revenue [120 340 210 95 160 80]}
(pj/lay-bar :department :revenue)
(pj/options {:x-tick-angle -45}))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))Lollipop with :color
Map a categorical column to :color to distinguish groups visually β here, products grouped by region.
(-> {:product ["A" "B" "C" "D" "E" "F"]
:revenue [120 95 150 80 200 110]
:region ["North" "South" "North" "South" "North" "South"]}
(pj/lay-lollipop :product :revenue {:color :region}))See Also
- Core Concepts β mappings and aesthetic vocabulary
- Distributions β when comparing categories also means comparing their distributions
Whatβs Next
- Change Over Time β line charts, step functions, and stacked areas
- Configuration β control dimensions, palettes, and themes