Categories &

Functions List

Function Reference: swarmchart

statistics: swarmchart (x, y)

statistics: swarmchart (x, y, sz)

statistics: swarmchart (x, y, sz, c)

statistics: swarmchart (…, 'filled')

statistics: swarmchart (…, name, value)

statistics: swarmchart (ax, …)

statistics: s = swarmchart (…)

Draw a swarm chart.

swarmchart (x, y) draws the points of y against x, spread sideways where they would otherwise sit on top of one another, so that the shape of each column shows where its values gather. sz sets the marker area and c its colour, as they do for scatter, and 'filled' fills the markers.

s is the scatter object drawn, and carries XJitter, YJitter, XJitterWidth and YJitterWidth besides everything a scatter object carries. Setting any of them spreads the points again and redraws.

NameValue
'XJitter'How the points are spread sideways: 'density', the default here, spreads each point by a uniform draw weighted by how many of its neighbours share its value, so a crowded part of a column spreads wide and a lone point hardly moves; 'rand' spreads every point alike; 'randn' draws the offsets from a normal distribution; 'none' leaves them in a line.
'YJitter'The same, upwards. The default is 'none'.
'XJitterWidth'How far a point may be moved sideways, in the units of the axis. The default is nine tenths of the smallest gap between distinct values of x.
'YJitterWidth'The same, upwards.

MATLAB leaves XData as it was given and spreads the points as it draws them. Octave’s scatter object has no such step, so this moves the values themselves: XData holds where each point was drawn rather than where it came from. The difference shows in XData and nowhere else, the chart being the same.

See also: scatter, boxchart, gscatter

Source Code: swarmchart

A swarm chart spreads the points of each column sideways, so the shape of a column shows where its values gather. A crowded part spreads wide and a lone point hardly moves.

 load fisheriris
 g = grp2idx (species);
 swarmchart (g, meas(:,1));
 xlabel ('species');
 ylabel ('sepal length');
plotted figure

A swarm sits over a box chart readily, the one showing the summary and the other every observation behind it.

 load fisheriris
 g = grp2idx (species);
 boxchart (g, meas(:,1));
 hold on
 swarmchart (g, meas(:,1), 10, [0.3, 0.3, 0.3]);
 hold off
 xlabel ('species');
 ylabel ('sepal length');
plotted figure

How far the points are spread, and by what rule, may be set afterwards.

 load fisheriris
 g = grp2idx (species);
 s = swarmchart (g, meas(:,1));
 set (s, 'XJitterWidth', 0.3);
plotted figure