File Exchange

## plotfdesign(T,G,var​argin)

version 1.0.0.0 (5.98 KB) by
Plots data from a factorial design

7 Downloads

Updated 06 Feb 2017

View License

I never much liked the 'multcompare' function (much less the boxplot that Matlab uses for ANOVA), so I wrote this one to produce nicer graphs for factorial designs,
H= plotfdesign(T,G) plots means of vector T, their 95% conf. interval (as a box) and st. deviations (as a line), for each combination of factors defined in G. T is a numeric vector of data. G is a numeric or cell array of strings defining the groups (factors) for each data value. It returns a figure handle H which allows to set the characteristics of each component. Up to 4 factors are allowed. The first factor will be represented by adjacent bars in a plot, while the second factor (if present) is shown as different groups of bars in the same plot. Factors 3 and 4 (if present) will be columns and rows of subplots, respectively, in the figure.
The raw data is plotted (jittered) over each bar. The color of the dots (gray by default) is set to red if their distribution is not normal according to Lilliefors test (alpha=0.05).

### Cite As

Francisco de Castro (2021). plotfdesign(T,G,varargin) (https://www.mathworks.com/matlabcentral/fileexchange/60615-plotfdesign-t-g-varargin), MATLAB Central File Exchange. Retrieved .

### Comments and Ratings (22)

Francisco de Castro

Hi LaraH. I don't see any files, or invitations, when I click in your link. Can you send the file directly to me? decastro.f at gmail
Thanks

LaraH

Dear Francisco,
Thanks for the quick feedback.
Here you can see my datafile https://drive.matlab.com/sharing/1cc09ffe-f9aa-4358-ab66-6f5beb4a5d18
When x and g is used as an input to the function, I get only one plot for the first factor. Thanks a lot!
Lara

Francisco de Castro

@LaraH. The way you describe your data should work. If you have two factors, there will be only one plot, with four groups (the second factor) of two bars each (the first factor). Could you send me a copy of the data, so I can check more?

LaraH

Hi Francisco,
Thank you for the excellent function!
I am attempting to plot data with two factors (2 & 4 levels) so that one plot contains 4 subplots with 2 adjecent boxes.
The function works but I only get one plot for one factor. I guess it is because of the way G is coded. In my case, I now have a 232x2Matrix, containing the values 1 and 2 (conditions of my first factor) in the first column, and values 1-4 (conditions of my second factor).
Does G need to be coded differently for the function to compute the 2x4 subplots?
Many thanks and very best wishes,
Lara

raym

Hi, Francisco,
You mistake the number 218, 153 as 128, 135.

raym

The reason I want to use number but not string for the second factor is that the second level factor shows on the x-axis. I thought your function could sort the content of the second level factors so that if I have 5,10,30,70 as second level factors, the x-axis will show up 5--10--30--70; but if I use string, the x-axis show up as 10--30--5--70, this is what I observed with string as second level factor.

raym

For example:
g= repmat(fullfact([2 2]),100,1);
g1= find(g(:,1)==1);
g2= find(g(:,1)==2);
x= zeros(size(g,1),1);
x(g1)= normrnd(10,1,numel(g1),1);
x(g2)= normrnd(5,3,numel(g2),1);
g = mat2cell(g,ones(400,1),ones(2,1));
g(cellfun(@(x) isequal(x,1),g(:,1)),1) = {'wt'};
g(cellfun(@(x) isequal(x,2),g(:,1)),1) = {'A30P'};
g(cellfun(@(x) isequal(x,1),g(:,2)),2) = {20000};%the second factor I want to use number here
g(cellfun(@(x) isequal(x,2),g(:,2)),2) = {70000};%the second factor I want to use number here
H= plotfdesign(x,g);

Error using cell/unique (line 85)
Input A must be a cell array of strings.

Error in plotfdesign (line 84)
uG= unique(G(:,j));
It says that function unique cannot take in numbers.
Could you try this:
>> unique({68,57,68,67,67})
Error using cell/unique (line 85)
Input A must be a cell array of strings.

I have to use strings as:
g(cellfun(@(x) isequal(x,1),g(:,2)),2) = {'20000'};
g(cellfun(@(x) isequal(x,2),g(:,2)),2) = {'70000'};

Francisco de Castro

Hi Mendel,
I'm sorry but I don't get the error you mention in my computer. It's quite weird because line 135 in plotfdesign is empty and line 128 in notBoxPlot is a comment. I cannot comment on your modifications to notBoxPlot, for that you need to contact its author directly. The line 162 you modify in plotfdesign comes from an example in notBoxPlot.
I don't quite understand your question about "xlabel, ylabel or title for all subplot?" The ylabel can be specified with optional argument 'vname' (for variable name), and the xlabels with optional argument 'fname' followed by a cell array. Is that what you mean? There is no argument to add a title to the whole figure. You'd need to do that after the figure is generated.

raym

After I put the two lines into try-end block, the line become as thick as box.

raym

Hi, Francisco,
How can I change the xlabel, ylabel or title for all subplot?
xlabel('---') only labels the last subplot, not the whole subplot.

raym

I put the two lines into try-end block, and it works now, but I still do not know if this will cause unforeseeable problems.

plotfdesign (line 162)
try
set([H(k).sd], 'Color',0.75*cmap(k,:),'LineWidth',2);
end

notBoxPlot (line 218)
try
params.parse(varargin{:});
end

raym

Hi

When I run the example in your mfile:
g= repmat(fullfact([2 2]),100,1);
g1= find(g(:,1)==1);
g2= find(g(:,1)==2);
x= zeros(size(g,1),1);
x(g1)= normrnd(10,1,numel(g1),1);
x(g2)= normrnd(5,3,numel(g2),1);

H= plotfdesign(x,g);

error appeared:
Error using notBoxPlot (line 218)
Expected a string for the parameter name, instead the input type was 'double'.
Error in plotfdesign (line 153)
H= notBoxPlot(yy,x(unique(g(rows,1))),0.7*bs,'sdline');

Nestor Matthews

Hi Francisco,
Yes, thank you for your help, and excellent function!
Nestor

Francisco de Castro

Hi Nestor,
The boxes for different levels of factor 1 will have different colors by default. You can specify any color map as:
H= plotfdesign(...,'cmap','prism')
Or provide your own color map.

The legend should be generated automatically witht he appropriate colors. Is this what you meant?
F

Nestor Matthews

Hello Again, Francisco,

Within your plotfdesign function, I'm attempting to manipulate the colors of the boxes that reflect the 95% Confidence Intervals. I'm using this syntax for with the LEGEND command for my two groups (Color Guard, and Low Brass).

legend({'Color Guard','Low Brass'},'Orientation','horizontal','Location','northwest');

Do you have a suggestion about the syntax for visually distinguishing the two (or 3) groups by color?

Thank you very much!
Nestor

Nestor Matthews

Wonderful. Thank you Francisco!

Francisco de Castro

Hi Nestor,
It is possible indeed. The N of each group is determined automatically from the second argument (G). You needn't to specify it.
Fj

Nestor Matthews

Is it possible to use this code when the two groups have unequal N? I haven't been able to figure out how to do that. Thank you!

Francisco de Castro

Hi,
That error means you don't have function notboxplot (FEX ID: #26508). Check "other requirements" in my function.
Good luck

Yongqiang Zhou

Hi Francisco, Thank you very much. The error is sth like this "Undefined function 'NBP.SEM_calc' for input arguments of type 'double'." I've tried on both MATLAB 2012a and 2015a.

Francisco de Castro

Hi Yongquiang,
The example works fine in my computer. If you could provide the error message you get I may be able to help.

Yongqiang Zhou

The example you provided in the function plotfdesign does not work. I don't know why.

##### MATLAB Release Compatibility
Created with R2015a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux
##### Acknowledgements

Inspired by: notBoxPlot

Inspired: jqu224/masterProject

### Community Treasure Hunt

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

Start Hunting!