Commit 83599581 authored by Tom's avatar Tom

Merge branch 'master' of

parents ae9cbe59 e6e77442
% it might be that 'patch' is a much better plotting function to plot the
% densities than 'area'; it seems to handle over-plotting of different
% areas with different zero lines very well. This would avoid the horrible
% gymnastics that the repeated measures raincloudplot currently has to deal
% with.
% This is a work-in-progress example inspired by this post on Matlab
% answers
%% TO-DO:
% Change YTick and YTicklabel to meaningful things (passed as input argument?)
% Option to rotate plot by 90'
% Patch can create colour gradients using the 'interp' option to
% 'FaceColor'. Allow this?
% h = toy_example_with_patch(data, colours)
% h is a cell array of the figure parts
function h = toy_example_with_patch(d,cl)
%% check dimensions of data
[nper, nseries] = size(d);
% make sure we have enough colours
assert(all(size(cl) == [nseries 3]), 'number of colors does not match number of plot series');
% NOTE: This way plots the first data in the series on the *bottom*
% Probably okay to hard-code this as it just determines the granularity of
% the density estimate
nbins = 200;
for i = 1:nper
for j = 1:nseries
% calculate kernel densities
[ks{i,j},x{i,j}] = ksdensity(d{i,j},'NumPoints',nbins);
% TO-DO: Switch here to use alternative methods to estimate density (e.g. RASH)
spacing = 2 * mean(mean(cellfun(@max,ks)));
ks_offsets = [0:nper-1] .* spacing;
for i = 1:nper
for j = 1:nseries
% calculate patch vertices from kernel density
verts{i,j} = [x{i,j}', ks{i,j}'+ ks_offsets(i); x{i,j}', ones(nbins,1)*ks_offsets(i)];
verts{i,j} = [x{i,j}', ks{i,j}'+ ks_offsets(i); x{i,j}', ones(nbins,1)*ks_offsets(i)];
% Define the faces to connect each adjacent f(x) and the corresponding points at y = 0.
q = (1:nbins-1)';
faces = [q, q+1, q+nbins+1, q+nbins];
%% jitter for the raindrops
% TO-DO: These should probably not be hardcoded either...
jitwidth = spacing / 8;
sz = 100;
for i = 1:nper
for j = 1:nseries
jit{i,j} = jitwidth + rand(1,length(d{i,j})) * jitwidth;
%% means
mnz = cellfun(@mean,d);
%% plot
% note - we *could* plot everything here in one big loop, but then
% different figure parts would overlay each other in a silly way.
figure; hold on
% patches
for i = 1:nper
for j = 1:nseries
% plot patches
h.p{i,j} = patch('Faces',faces,'Vertices',verts{i,j},'FaceVertexCData',[cl(j,:)],'FaceColor','flat','EdgeColor', 'none', 'FaceAlpha',0.5);
% scatter rainclouds
h.s{i,j} = scatter(d{i,j},-jit{i,j} + ks_offsets(i),'MarkerFaceColor',cl(j,:),'MarkerEdgeColor','none', 'MarkerFaceAlpha', 0.5, 'SizeData', sz);
% plot mean lines
for i = 2:nper % note 2, not 1. We have nper-1 lines because lines connect pairs of points
for j = 1:nseries
line(mnz([i-1 i],j),ks_offsets([i-1 i]),'LineWidth', 4,'Color',cl(j,:));
% plot mean dots
for i = 1:nper
for j = 1:nseries
h.m(i,j) = scatter(mnz(i,j),ks_offsets(i),'MarkerFaceColor',cl(j,:),'MarkerEdgeColor',[0 0 0], 'MarkerFaceAlpha', 1, 'SizeData', sz*2, 'LineWidth', 2);
%% clear up axis labels
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment