按 2 个变量分组,一个变量具有独特的颜色,另一个变量具有独特的形状

Group by 2 variables, with unique colors for one and unique shapes for the other

我正在尝试在相关图中绘制一组配对数据。这是一项通过两种田间处理和 8 个输入水平来判断植物反应的研究。我想显示数据,用 8 种不同的颜色表示 8 个输入级别,用 3 个不同的形状表示 3 个不同年份的研​​究。我正在使用 gscatter。

问题是,当我指定两个变量作为分组依据时,它不知道颜色与一个变量相关而形状与另一个变量相关,我只想在输入改变时改变颜色,并且只形状随着年份的变化而变化。最终结果是,对于每个独特的分组(其中 24 个),它通过每个独特的组合同时通过颜色和形状前进。

这里有两张图来说明输出。第一张图显示了所有刚刚按输入分组和着色的数据。它正确地为每个输入级别分配了唯一的颜色。 3 年每年 2 分,每种颜色共计 6 分。

现在我只想改变第 2 年和第 3 年的形状并保持颜色不变,但就是我得到的。您可以在图例中看到它如何同时循环 3 次颜色和 8 次形状。所以不同的颜色被分配到相同的输入级别。我尝试以不同的方式对数据进行排序,但得到的结果完全相同。

我也尝试手动调整每个数据点的颜色,但对象中的顺序必须与图例中列出的顺序不同,因为我得到了奇怪的结果。有些点被正确更改,但其他点没有。

一定有更好的方法来做到这一点。我愿意接受所有建议,无论是让这个方法起作用,还是使用不同的函数。

这是包含较小数据子集的代码:

clear
Treatments = table([{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'};{'T1'};{'T2'}]);
Data = [2016    2016    2017    2017    2018    2018    2016    2016    2017    2017    2018    2018    2016    2016    2017    2017    2018    2018;...
    0   0   0   0   0   0   1   1   1   1   1   1   2   2   2   2   2   2;...
    4704.5  4059.5  10891   11440.5 4083.5  2876    11459.66667 11752   11566   12036   11323.5 11118.5 10296.5 10234   13074.5 14166   9062    9669]';

% split by treatment
t1Response = Data(strcmp(Treatments.Var1,'T1'),3);
t2Response = Data(strcmp(Treatments.Var1,'T2'),3);
Inputs = Data(strcmp(Treatments.Var1,'T2'),2); % treatment doesn't matter, just need one set
Years = Data(strcmp(Treatments.Var1,'T2'),1); % treatment doesn't matter

% all points one shape, group colors just by inputs
figure;
colors = lines(8);
colors(8,1)=0.5;
g = gscatter(t1Response,t2Response,Inputs,colors([7,2,3],:),'.',20,'on');

% group by input and year
figure;
g2 = gscatter(t1Response,t2Response,{Inputs,Years},colors([7,2,3],:),'.s^',20,'on'); g2(1).MarkerFaceColor = colors(7,:);
% g2(1).MarkerFaceColor = colors(7,:);
% g2(2).MarkerFaceColor = colors(7,:);
% g2(3).MarkerFaceColor = colors(7,:);
% g2(4).MarkerFaceColor = colors(2,:);
% g2(5).MarkerFaceColor = colors(2,:);
% g2(6).MarkerFaceColor = colors(2,:);
% g2(7).MarkerFaceColor = colors(3,:);
% g2(8).MarkerFaceColor = colors(3,:);
% g2(9).MarkerFaceColor = colors(3,:);

您可能不得不放弃 gscatter,自己动手。来自多个分组变量的 documentation 项:

Alternatively, g can be a cell array containing several grouping variables (such as {g1 g2 g3}), in which case observations are in the same group if they have common values of all grouping variables.

即它无法处理具有独立颜色和形状的独立变量。

您可以使用 fingroups 和一些索引来建立组并循环多年进行绘图。这是一个强大的解决方案,可以处理比 colours/markers:

更多组的情况
% Define colours and markers
colors = lines(8); colors(8,1)=0.5;
markers = {'x','d','o','+','*','s','p''h'};

% Create colour matrix
[gInputs, uInputs] = findgroups(Inputs);
if max(gInputs) > size(colors,1)
    warning( 'More inputs than possible colors, colors will be re-used' );
end
colors = colors(mod(gInputs-1,size(colors,1))+1,:); % mod to handle out of range case
% Create marker array
[gYears, uYears] = findgroups(Years);
if max(gYears) > numel(markers)
    warning( 'More years than possible markers, markers will be re-used' );
end
markers = markers(mod(gYears-1,numel(markers))+1); % mod to handle out of range case

figure(); hold on
for iYr = 1:max(gYears)
    idx = (iYr == gYears); 
    scatter(t1Response(idx), t2Response(idx), 20, colors(idx,:), 'Marker', markers{iYr}, 'displayname', num2str(uYears(iYr)), 'LineWidth', 2 );
end
hold off
legend('show')

如果您希望图例反映年份和输入的组合,那么您将不得不使用双循环

% Setup as above...
figure(); hold on
for iYr = 1:max(gYears)
    for iIn = 1:max(gInputs)
        idx = (iYr == gYears) & (iIn == gInputs); 
        scatter(t1Response(idx), t2Response(idx), 20, colors(idx,:), 'Marker', markers{iYr}, 'displayname', sprintf('%d: %d',uYears(iYr),uInputs(iIn)), 'LineWidth', 2 );
    end
end
hold off
legend('show')

您可以使用 line 而不是 scatter 使最后一个示例更快,但语法略有不同,因此我保留它以避免复杂化。

要获得更简洁的图例,您必须使用您想要的格式伪造一些行。我对代码进行了一些重组以展示如何做到这一点:

% Define colours and markers
colors = lines(8); colors(8,1)=0.5;
markers = {'x','d','o','+','*','s','p''h'};

% Create colour matrix
[gInputs, uInputs] = findgroups(Inputs);
if max(gInputs) > size(colors,1)
    warning( 'More inputs than possible colors, colors will be re-used' );
end
% Create marker array
[gYears, uYears] = findgroups(Years);
if max(gYears) > numel(markers)
    warning( 'More years than possible markers, markers will be re-used' );
end
% handle index out of range
markers = markers(mod((1:max(gYears))-1,numel(markers))+1);
colors = colors(mod((1:max(gInputs))-1,size(colors,1))+1,:);
% Setup as above...
lineProps = {'markersize', 5, 'linestyle', 'none', 'LineWidth', 2};
figure(); hold on
for iYr = 1:max(gYears)
    for iIn = 1:max(gInputs)
        idx = (iYr == gYears) & (iIn == gInputs); 
        line(t1Response(idx), t2Response(idx), 20, 'color', colors(iIn,:), 'Marker', markers{iYr}, 'handlevisibility', 'off', lineProps{:} );
    end
end
% Spoof markers for the legend
for iYr = 1:max(gYears)
    line( NaN, NaN, 'color', 'k', 'Marker', markers{iYr}, 'displayname', num2str(uYears(iYr)), lineProps{:} );
end
for iIn = 1:max(gInputs)
    line( NaN, NaN, 'color', colors(iIn,:), 'Marker', 'o', 'markersize', 20, 'displayname', num2str(uInputs(iIn)), lineProps{:} );
end
hold off
legend('show')