Grouped scatter with categorical variables

10 views (last 30 days)
Hi, I want to create a grouped scatter plot with two levels of categorical variables - one for the X axis and one to group the plotted points. I'm using the following command: gscatter(Zone, alpha_1, group) where Zone is my X axis and takes 4 values (E2, EG, SE2 and SW2), alpha_1 is a 508x1 vector of numerical values and group is a 508x1 categorical vector (Monday, Tuesday, Wednesday, Thursday, Friday). However, I get the error:
Error using categorical/min (line 35) Relational comparisons are not allowed for categorical arrays that are not ordinal.
Error in gscatter (line 130) xlim(1) = min(xlim(1), min(min(x))-0.05*d);__
If I replace Zone with a vector of numbers 1-4 (with each category assigned a number) it plots fine and I get the plot shown below. This is what I want except with the X axis labelled with the Zone name - E2 etc. Can anyone explain what the error means and how to get around it if possible?
Many thanks for any help

Accepted Answer

Guillaume
Guillaume on 19 May 2016
The error message is fairly clear. gscatter tries to order your categories but since they're not ordinal, there is no defined ordering.
As per the help of categorical, you need to specifically say that a categorical array is ordinal when you create it. So create your zone with for example:
zones = categorical(source, 'Ordinal', true);
As the help says if 'Ordinal' is false (the default), "the categories of B have no mathematical ordering. Therefore, you can only compare the values in B for equality."
  3 Comments
Guillaume
Guillaume on 19 May 2016
gscatter is trying to subtract a value from your categorical x. Subtraction has no meaning for categorical so matlab does not define it.
It looks like you can't use categorical with gscatter for the x and y. I'm afraid you're going to have to use numerical values for your zones and relabel the axis afterward
set(gca, {'XTick', 'XTickLabel'}, {1:4, {'E2', 'EG', 'SE2', 'SW2'}})
Rebecca Ward
Rebecca Ward on 19 May 2016
And funnily enough it does seem to plot, but doesn't label the X axis correctly?
As you suggest the manual is useful - my mistake was that as I'm not interested in the order, I didn't equate the 'Ordinal' command to be necessary in my case - but of course MATLAB needs that specified. So thanks again for pointing that out.

Sign in to comment.

More Answers (0)

Categories

Find more on Categorical Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!