フィルターのクリア

Grouped scatter with categorical variables

15 ビュー (過去 30 日間)
Rebecca Ward
Rebecca Ward 2016 年 5 月 19 日
コメント済み: Rebecca Ward 2016 年 5 月 19 日
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 Center および File ExchangeScatter Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by