MATLAB Answers

How to use 'factorgap' to set larger gaps between groups of boxplot.

조회 수: 3(최근 30일)
Levente Gellért
Levente Gellért 2021년 7월 26일
댓글: Levente Gellért 2021년 7월 26일
Dear Community,
I am using the below code to combine boxplot with scatter plot. I am wondering, how to use the 'factorgap' to have every boxes in a kind of groups of two, that is, larger gap after every second ones.
I appreciate your suggestions!
Regards
lg
h=boxplot(vertcat(C{:}),grp,'symbol','','Colors','k');
set(h,'LineWidth',1.5)
lines = findobj(gcf, 'type', 'line', 'Tag', 'Median');
set(lines, 'Color', 'r');
hold on
scatter(grp,data_for_scatter,'.','k','jitter','on', 'jitterAmount',0.1);
ps: Please find the example data attached!
  댓글 수: 3
Levente Gellért
Levente Gellért 2021년 7월 26일
Nice, many thanks!
Regards
lg

댓글을 달려면 로그인하십시오.

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 7월 26일
편집: Scott MacKenzie 2021년 7월 26일
It is important to distinguish between a group and the members or elements of that group. The documentation on this point is poor for the boxplot function, and there are no examples. But, here's the scoop...
In order to use factorgap, you need >1 group. The factorgap setting allows you to control the spacing between the different groups in a boxplot. But, you only have one group (grp). Yes, there are 13 members or elements for that group, but there is only one group or grouping variable. Make sense?
To illustrate, here's a box plot for a data set that is divided into two groups. There are three members the 1st group and two members for the 2nd group. Using factorgap, extra spacing is inserted between the members, or boxes, for the 1st group. You can see this in the chart that follows.
% test data
M = rand(100,100);
% two group variables
g1 = randi([1 3],100,1); % 3 members for g1
g2 = randi([8 9],100,1); % 2 members for g2
% create box plot with extra spacing between the g1 boxes
boxplot(M, [g1 g2],'factorgap', 15);
If you want some extra spacing after every 2nd box, then you need to create a 2nd group variable and include it in your code, as demonstrated above.

추가 답변(0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by