Grouped scatter with categorical variables

13 次查看(过去 30 天)
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

采纳的回答

Guillaume
Guillaume 2016-5-19
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 个评论
Guillaume
Guillaume 2016-5-19
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 2016-5-19
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Categorical Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by