How would you produce in Matlab a categorical scatter plot comparable to this?
The above chart was generated in R, in response to this question.
How would you produce in Matlab a categorical scatter plot comparable to this?
The above chart was generated in R, in response to this question.
You could set the 'XJitter'
argument on a normal scatter
plot, as documented here.
The 'XJitterWidth'
argument can be modified to get wider or narrower point clouds.
For example, the image below was created as follows:
% create dummy data
n = 100;
x = [ones(n,1), 2*ones(n,1), 3*ones(n,1)];
y = rand([n, 3]); % uniform distribution
% create plot
scatter(x, y, 'XJitter', 'rand', 'XJitterWidth', 0.2);
Not sure when this option was first introduced, but it is also used in the swarmchart function, which was introduced in Matlab R2020b:
swarmchart(x, y, 'XJitterWidth', 0.2);
Here's how the jitter is implemented, according to the docs:
The points in a swarm chart are jittered using uniform random values that are weighted by the Gaussian kernel density estimate of y and the relative number of points at each x location. This behavior corresponds to the default 'density' setting of the XJitter property on the Scatter object when you call the swarmchart function.
The maximum spread of points at each x location is 90% of the smallest distance between adjacent x values by default:
spread = 0.9 * min(diff(unique(x)));
To see how to plot lines for the mean or median values, have a look at e.g. this answer.
If you're interested in two-sample comparisons, have a look at the gardnerAltmanPlot function (introduced in R2022a). This includes a confidence interval for the difference.
I know this post is old, but I recently updated this function, that you may find useful, since it distributes the points the same way always and allows for very high personalization of the shape, colors and distributions of the points. I think it reproduces quite well the shape of those graphs showed in some publications. Have a look if you are interested
http://www.mathworks.com/matlabcentral/fileexchange/54243-univarscatter
You can use the undocumented
jitter
property ofscatter
:This results in: