Grouped scatter with categorical variables

13 Ansichten (letzte 30 Tage)
Rebecca Ward
Rebecca Ward am 19 Mai 2016
Kommentiert: Rebecca Ward am 19 Mai 2016
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

Akzeptierte Antwort

Guillaume
Guillaume am 19 Mai 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 Kommentare
Guillaume
Guillaume am 19 Mai 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 am 19 Mai 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Categorical Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by