不相等样本的箱形图之间的距离 [英] distance between box plots with unequal samples

查看:114
本文介绍了不相等样本的箱形图之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用不相等的样本"绘制条形图.这是示例代码

I would like to draw a bar chart with "unequal samples". Here is an example code

 A = [16 20 15 17 22 19 17]';
 B = [22 15 16 16 16 18]';
 C = [23 9 15 18 13 27 17 14 16 15 21 19 17]';
 group = [    ones(size(A));
     2 * ones(size(B));
     3 * ones(size(C))];
 figure
boxplot([A; B; C],group)
set(gca,'XTickLabel',{'A','B','C'})

输出如下:

但是,我希望第1组与第3组之间有一个距离.与下图相同:(此图只是来自另一个来源的复制粘贴,但每个图箱之间的距离组可见)

However, I would like to have a distance between group1,2 with group 3. As same as what you see in the figure below:(this figure is just a copy paste from another source but the distance between box plot of each group is visible)

我试图在此类命令中使用'factorgap'

I tried to use 'factorgap' in such command

 figure
 boxplot([A; B; C ],group,'factorgap',[50,1])

但是,由于每个组中的样本数量不同,因此无法正常工作.

However, because the number of samples in each group is different it did not work.

有什么建议吗?

推荐答案

事实上,我建议您使用的第一个解决方案是一个小的解决方法,其中包括在第二个和第三个之间插入另一个不可见的组:

The first solution I propose you is in fact a small workaround that consists in inserting another, invisible group between the second and the third one:

A = [16 20 15 17 22 19 17]';
B = [22 15 16 16 16 18]';
C = [23 9 15 18 13 27 17 14 16 15 21 19 17]';

group = [
  ones(size(A));
  2 * ones(size(B));
  3;
  4 * ones(size(C))
];

figure();
boxplot([A; B; NaN; C],group);
set(gca,'XTickLabel',{'A','B','','C'});

以下是输出:

现在,让我们建立一些严肃的东西:

Now, let's build up something serious:

% Define the sample data...
A = [16 20 15 17 22 19 17]';
B = [22 15 16 16 16 18]';
C = [23 9 15 18 13 27 17 14 16 15 21 19 17]';

% Find the length of the largest vector...
A_len = numel(A);
B_len = numel(B);
C_len = numel(C);
max_len = max([A_len B_len C_len]);

% Transform vectors into fixed size vectors of length max_len...
A = [A; NaN(max_len - A_len,1)];
B = [B; NaN(max_len - B_len,1)];
C = [C; NaN(max_len - C_len,1)];

% Define labels and groups...
L1 = [repmat('A',1,numel(A)),repmat('B',1,numel(B))];
L2 = repmat('C',1,numel(C));
L = [L1 L2];
G = [repmat('1',1,numel(L1)),repmat('2',1,numel(L2))];

% Plot the boxes...
boxplot([A B C],{G';L'},'FactorGap',50);

以下是输出:

这篇关于不相等样本的箱形图之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆