分层分组的箱线图 [英] Hierarchically grouped boxplot

查看:96
本文介绍了分层分组的箱线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个箱形图,其中包含两组数据进行比较.我愿意使用分层分组的箱线图.我可以使用此函数绘制一组数据.我想知道如何使用此功能将两组数据绘制在一起.我用红色手工绘制了第二组数据,以显示我要绘制的内容!

I would like to draw a boxplot with two sets of data to compare. I am willing to use Hierarchically grouped boxplot. I could just plot one set of my data using this function. I was wondering how I can use this function to plot two sets of data together. I drew the second set of data in red one by hand to show what I am trying to plot!

我的问题是我无法使用hold on在一组图表上放置两组数据.

My problem is that I can't put two sets of data on one graph with hold on.

推荐答案

好吧,这并不是您所要的,也不使用功能hierarchicalBoxplot,但这可能是一种解决方法.我使用MATLAB示例数据进行了演示:

Well, this is not precisely what you asked for, and does not use the function hierarchicalBoxplot, but it may be a workaround. I demonstrate it using MATLAB example data:

load carsmall
% cleaning the data a little
Origin = categorical(cellstr(Origin));
MPG(Origin=='Italy') = [];
Origin(Origin=='Italy') = [];
% this part is just for readability:
data1 = MPG;
groups1 = Origin;
data2 = MPG*3;
groups2 = Origin;
% And we start:
% =============
% we need a wider figure, with a white background:
figure('Color',[1 1 1],'Position',[178 457 1114 521])
main_ax = axes; % create a tmporary axes
% we get the measurements of the ploting area:
pos = main_ax.Position;
% and divide it to our data:
group_number = 6;
width = pos(3)/group_number; % the width of each group
% the bottom left corner of each group:
corner = linspace(pos(1),pos(3)+pos(1),group_number+1);
clf % clear the area!
% Now we plot everything in a loop:
for k = 1:group_number
    % create a different axes for each group:
    ax = axes;
    boxplot(ax,data1,groups1); % plot the first set
    hold on
    boxplot(ax,data2,groups2) % plot the second set
    % set the ylim to include all data:
    ax.YLim = [min([data1; data2])-5 max([data1; data2])+10];
    ax.XTickLabelRotation = 90; % rotate xlables if needed
    box off
    if k == 1 
        ylabel('Miles per Gallon (MPG)') % only for the most right axes 
    else
        ax.YTick = [];
    end
    xlabel(['Group ' num2str(k)])
    ax.Position = [corner(k) 0.2 width 0.7];
end
% and finally we place the title:
main_ax = axes('Position',[corner(1) 0.11 width*group_number 0.815]);
title('Miles per Gallon by Vehicle Origin')
axis off

% and this will color the data:
f = gcf;
colors = [1 0 0;0 0 1]; % red and blue
for g = 2:numel(f.Children)
    for k = 1:numel(f.Children(g).Children(1).Children)
        f.Children(g).Children(1).Children(k).Color = colors(1,:);
        f.Children(g).Children(1).Children(k).MarkerEdgeColor = colors(1,:);
        f.Children(g).Children(2).Children(k).Color = colors(2,:);
        f.Children(g).Children(2).Children(k).MarkerEdgeColor = colors(2,:);
    end
end

所有此过程给出:

可能需要做一些最后的调整,但这是从;)开始的事.

It will probably need some final tweaks, but it's somthing to start from ;)

要获得并排视图,您可以将所有组绘制在一起,只需移动x线即可:

For a side by side view, you can plot all groups together, and just move the x-ticks:

% Making some data:
% MAKE SURE YOU UNDERSTAND HOW THE DATA IS ARRANGED WITHIN THE GRAPH
years = 6; % try to change this number
groups = 5; % try to change this number
data1 = rand(100,years);
data2 = rand(100,years)+0.3;
groups1 = randi(groups,100,1)*2-1; % groups 1 3 5 7 9
groups2 = randi(groups,100,1)*2; % groups 2 4 6 8 10
legendEntries = {'A' 'B'};
colors = [1 0 0;0 0 1]; % red and blue

% And we start:
% =============
% we need a wider figure, with a white background:
figure('Color',[1 1 1],'Position',[178 457 1400 521])
main_ax = axes; % create a temporary axes
% we get the measurements of the plotting area:
pos = main_ax.Position;
% and divide it to our data:
width = pos(3)/years; % the width of each group
% the bottom left corner of each group:
corner = linspace(pos(1),pos(3)+pos(1),years+1);
clf % clear the area!
% Now we plot everything in a loop:
for k = 1:years
    % create a different axes for each group:
    ax = axes;
    boxplot(ax,[data1(:,k); data2(:,k)],[groups1; groups2]);
    ax.XTick = 1.5:2:(groups*2-0.5); % to "combine" the groups in pairs
    ax.XTickLabel = {'a','b','c','v','f'};
    % set the ylim to include all data:
    ax.YLim = [min([data1(:); data2(:)]) max([data1(:); data2(:)])];
    box off
    if k == 1 
        ylabel('Miles per Gallon (MPG)') % only for the most right axes 
    else
        ax.YTick = [];
    end
    xlabel(num2str(2000+k)) % the labels for the years
    ax.Position = [corner(k) 0.11 width 0.8];
    % this will color the data:
    for g = 1:2:numel(ax.Children.Children)-1
       ax.Children.Children(g).Color = colors(1,:);
       ax.Children.Children(g).MarkerEdgeColor = colors(1,:);
       ax.Children.Children(g+1).Color = colors(2,:);
       ax.Children.Children(g+1).MarkerEdgeColor = colors(2,:);
    end
    if k == years
        % you can try to change here the index to 1:2 and see if you like it:
        leg = legend(ax.Children.Children(20:21),legendEntries);
        leg.Position(1) = 0.92;
    end
end
% and finally we place the title:
main_ax = axes('Position',[corner(1) 0.11 width*years 0.815]);
title('Miles per Gallon by Vehicle Origin')
axis off

我们得到了拥挤的情节:

And we get the crowded plot:

这篇关于分层分组的箱线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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