-
-
Save harimau99/300e50c81ac22351a42df71f3a867352 to your computer and use it in GitHub Desktop.
dotplot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [sh, lh, mp] = dotPlot(X, G, Wid, med, beas) | |
% [sh, lh, mp] = dotPlot(X, G, Wid, med, beas) | |
% | |
% load fisheriris.mat | |
% [sh, lh, mp] = dotPlot(meas(:,3), species, .25, false, true) | |
% | |
% input: | |
% X: data | |
% G: Group | |
% Wid: width of dot distribution (default: .35) | |
% med: flag of lineplot by median (default: false, mean) | |
% | |
% output: | |
% sh: handle of scatter plot | |
% lh: handle of mean (median) line plot | |
% mp: mean (median) value of each group | |
% | |
% Ryosuke F Takeuchi 2017 | |
switch nargin | |
case 2 | |
Wid = .35; | |
med = 0; | |
beas= 0; | |
case 3 | |
med = 0; | |
beas = 0; | |
case 4 | |
beas = 0; | |
end | |
xvar = unique(G); | |
for i = 1:length(xvar) | |
if iscell(xvar) | |
xx = strcmp(G, xvar{i}); | |
else | |
xx = G==xvar(i); | |
end | |
[xa, Xo(xx)] = dotw(X(xx), Wid); | |
if med | |
mp(i) = nanmedian(X(xx)); | |
else | |
mp(i) = nanmean(X(xx)); | |
end | |
if ~beas | |
Xo(xx)=X(xx); | |
end | |
sh(i) = scatter(i+xa, Xo(xx), '.', 'SizeData', 60); hold on; | |
lh(i) = plot(i+[-1.4 1.4]*Wid, mp(i)*[1 1], ... | |
'k', 'LineWidth', 1.75); | |
end | |
if iscell(xvar) | |
set(gca, 'XTick', 1:length(xvar), 'XTickLabel', xvar); | |
end | |
end | |
function [xb, Xo] = dotw(X, Width) | |
xb = X*0; | |
Xo = X; | |
[N, Edge] = histcounts(X, round(length(X)/3)); | |
Nn = Width*N/max(N); | |
for i = 1:length(N) | |
poi = X >= Edge(i) & X < Edge(i+1); | |
if i == length(N) | |
poi = X >= Edge(i) & X <= Edge(i+1); | |
end | |
xb(poi) = linspace(-Nn(i), Nn(i), N(i)); | |
if sum(poi) ==1 | |
xb(poi) = 0; | |
end | |
tmpX = X(poi); | |
tmpX = sort(tmpX); | |
Xo(poi) = tmpX(:); | |
% Order in bin | |
tmpX = X(poi); | |
tmpX = sort(tmpX); | |
tmpxb = xb(poi); | |
[~, I] = sort(abs(tmpxb)); | |
Xo(poi) = tmpX; | |
xb(poi) = sign(tmpxb(I)).*sort(abs(tmpxb))... | |
-median(sign(tmpxb(I)).*sort(abs(tmpxb))); | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment