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
0 个评论
采纳的回答
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
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'}})
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Categorical Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!