gramm icon indicating copy to clipboard operation
gramm copied to clipboard

Lines joining when grouping varible has large number of categories

Open JKHopf opened this issue 4 years ago • 1 comments

Hi Pier,

Thank you for a wonderful toolbox!

I seem to have found a minor bug when using geom_line with a grouping variable for colour that has >15 categories - the lines all join up to become one continuous line:

% grouping variable for colour
col = 1:16;

% create 2D arrays
x = repmat(1:10,16,1);
y = [];
for i = 1:length(col)
    y(i,:) = col(i)*(x(1,:).^2) + col(i)*100;
end

% create 1D column arrays
x2 = reshape(x',[],1);
y2 = reshape(y',[],1);
col2 = reshape(repmat(col,10,1),[],1);

% draw glitchy figure
g1 = gramm('x', x2, 'y', y2, 'color', col2);
g1.geom_line();
figure()
g1.draw();

image

The bug goes away when the number of categories is <16. With this number of categories it also plots the colors as discrete in the legend, while they are continous in all other cases. The latter is not an issue in my specific example/application, but I'm not sure how it handles when the categories are not numeric and >16.

g4 = gramm('x', x2, 'y', y2, 'color', col2, 'subset', col2<16);
g4.geom_line();
figure()
g4.draw();

image

I can work around the problem using 'group':

g3 = gramm('x', x2, 'y', y2, 'color', col2, 'group', col2);
g3.geom_line();
figure()
g3.draw();

Or by having the input data as 2D arrays rather than column vectors:

g2 = gramm('x', x, 'y', y, 'color', col);
g2.geom_line();
figure()
g2.draw();

The last two both produce the same image: image

However, it seems like a bit of a strange cutoff value? Has this been done for a particular reason or is it a genuine bug?

Cheers

JKHopf avatar Jun 11 '20 00:06 JKHopf

Hi,

Yes I did chose 15 as a cutoff value for switching between color as categories and colors as continuous values, the thinking being that it's around that number of groups that the colors become indissociable anyway. I do agree it's a bit arbitrary. An other way to solve the issue and keep more than 15 categories (apart from what you found), is to provide a categorical variable or a cell of strings to specify the color (i.e. something non numeric), it should work correctly.

I do agree that even all that said, your first figure is quite unexpected, I'll try to look if it's an easy fix.

piermorel avatar Aug 27 '20 14:08 piermorel