Commit d0b82399 authored by Tom Marshall's avatar Tom Marshall

added function for making raincloud plots

parent 3fffecea
% download open dataset from Marcus Munafo
% https://data.bris.ac.uk/datasets/112g2vkxomjoo1l26vjmvnlexj/2016.08.14_AnxietyPaper_Data%20Sheet.csv
mydir = 'insert path here';
myfile = '2016.08.14_AnxietyPaper_Data Sheet.csv';
f = fopen(fullfile(mydir,myfile));
dall = textscan(f,'','HeaderLines',1,'delimiter',',');
% just get the Anger, Disgust, Fear, and Happy conditions
d = [dall{28} dall{29} dall{30} dall{31}];
fclose(f);
%% get nice colours from color brewer
% (https://uk.mathworks.com/matlabcentral/fileexchange/34087-cbrewer---colorbrewer-schemes-for-matlab)
[cb] = cbrewer('qual','Set3',10,'pchip');
%% plot
figure;
subplot(4,1,1), raincloud_plot(d(:,1), cb(5,:));
subplot(4,1,2), raincloud_plot(d(:,2), cb(7,:));
subplot(4,1,3), raincloud_plot(d(:,3), cb(6,:));
subplot(4,1,4), raincloud_plot(d(:,4), cb(4,:));
\ No newline at end of file
function h = raincloud_plot(X,cl)
[a,b] = ksdensity(X);
wdth = 0.8; % width of boxplot
% TODO, should probably be some percentage of max.height of kernel density plot
% density plot
h{1} = area(b,a); hold on
set(h{1}, 'FaceColor', cl);
set(h{1}, 'EdgeColor', [0.1 0.1 0.1]);
set(h{1}, 'LineWidth', 2);
% make some space under the density plot for the boxplot
yl = get(gca,'YLim');
set(gca,'YLim',[-2 yl(2)]);
% jitter for raindrops
jit = (rand(size(X)) - 0.5) * wdth;
% info for making boxplot
Y = quantile(X,[0.25 0.75 0.5 0.02 0.98]);
% 'box' of 'boxplot'
h{2} = rectangle('Position',[Y(1) -1-(wdth*0.5) Y(2)-Y(1) wdth]);
set(h{2},'EdgeColor','k')
set(h{2},'LineWidth',2);
% could also set 'FaceColor' here as Micah does, but I prefer without
% mean line
h{3} = line([Y(3) Y(3)],[-1.2 -0.8],'col','k','LineWidth',2);
% whiskers
h{4} = line([Y(2) Y(5)], [-1 -1],'col','k','LineWidth',2);
h{5} = line([Y(1) Y(4)],[-1 -1],'col','k','LineWidth',2);
% raindrops
h{3} = scatter(X,jit - 1);
h{3}.SizeData = 5;
h{3}.MarkerFaceColor = cl;
h{3}.MarkerEdgeColor = 'none';
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment