Charting a Simulation
This guide is a set of recipes for turning a random sample into a chart a reader can watch
change: counting draws into categories, binning them into intervals, and comparing two
samples side by side. It assumes you already know how to write a <chart> — see the
<chart> reference for its attributes and properties.
Every recipe below counts the sample in DoenetML rather than inside the chart, so the counts
stay readable: $counts can go into a sentence, a table, or an <answer>
beside the picture.
Count draws into named categories
<tally> counts how many values fall into each category, and
<chart> draws the counts. The pieces are: draw the sample, map each draw onto a
category, count, chart.
Five tags hold the whole simulation:
<sampleRandomNumbers> draws the sample,
<cumulativeSum> accumulates the subpopulation sizes,
<searchSorted> maps every draw onto the subpopulation it
landed in, <tally> counts them, and <chart> draws the
counts.
Give <tally> its categories rather than leaving it to discover them. A subpopulation
that draws nothing then still counts zero and keeps its bar, instead of dropping out and
shifting the others along — which would make the chart jump every time the reader resamples.
<callAction> redraws the sample, and everything downstream follows.
Bin a continuous sample into intervals
For a measurement rather than a category, <binCounts> counts
how many values fall between each pair of cut points.
The cut points and the category names are two different lists: bins says where the
intervals begin and end, and categories says what to write under each bar. Naming each bin
by its midpoint, as here, keeps the axis readable; naming it by its range works too.
Compare two samples in one chart
Give the chart a <series> per sample. Each is counted separately and drawn in its own
color, and its <label> names it in the legend.
Both series are binned against the same cut points, which is what makes the bars comparable: a bar in one series stands next to the bar for the same interval in the other.
Use layout="stacked" instead when the two samples make up a whole and the total is
what a reader should read.
Ask about what the chart shows
Because the counts are components, the same numbers that drew the chart can be asked about.
$c.values is every value the chart drew, so a summary can be written beside the
picture and stay correct when the reader resamples.