15 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]
;; Tablecloth -- dataset manipulation
[tablecloth.api :as tc]
;; 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))Ranked
A ranking chart is read down from the biggest, and the bars above are in whatever order the data happened to hold. Plotje draws the bands of a categorical y axis in data order, running bottom to top, so sorting the rows ascending by the value puts the biggest bar at the top. The same sort does the same thing under (pj/coord :flip), which is how the count-bar form gets there.
(def sales-ranked
(-> sales
tc/dataset
(tc/order-by :revenue :asc)))(-> sales-ranked
(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
A stem and a dot in place of a bar β a lollipop chart draws the same comparison with less ink.
Vertical
(-> sales
(pj/lay-lollipop :product :revenue))Horizontal
Flipped for horizontal orientation.
(-> sales
(pj/lay-lollipop :product :revenue)
(pj/coord :flip))With a color column
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