Categorical scatter plot in Matlab

9k views Asked by At

How would you produce in Matlab a categorical scatter plot comparable to this?

Categorical scatter plot in R

The above chart was generated in R, in response to this question.

3

There are 3 answers

0
H.Muster On BEST ANSWER

You can use the undocumented jitter property of scatter:

% create example data
ydata = rand(50, 3)*2+2; 

[r, c] = size(ydata);

xdata = repmat(1:c, r, 1);

% for explanation see 
% http://undocumentedmatlab.com/blog/undocumented-scatter-plot-jitter
scatter(xdata(:), ydata(:), 'r.', 'jitter','on', 'jitterAmount', 0.05);

hold on;

plot([xdata(1,:)-0.15; xdata(1,:) + 0.15], repmat(mean(ydata, 1), 2, 1), 'k-')

ylim([0 max(ydata(:)+1)])

This results in:

Example figure

0
djvg On

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.

swarmchart example

0
Manuel Lera Ramírez On

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

Image of the plots