%% Plot average vars for all groups
% Base on logic in triggerOnDeath2019
function plotGroupsPanels(data)

    outDir = "D:\Harvard\Manuscripts\Summit paper\Figures\Figure 1\";
    %outDir = "D:\Harvard\Manuscripts\Summit paper\Figures\Fig 1 Supp\";   
    %outDir = "D:\Harvard\Manuscripts\Summit paper\Figures\Fig 1 Supp\";
    data.xspace = 1:1:data.params.lastFrames;
    data.xspace = downsample(data.xspace,data.params.downSamp);

    width = 100;
    height = 34;
    
    speedConv = speedToMmPerS;
    
    for sPlot=1:2
        figure('Units','Pixels','Position',[0 0 (width*5)+100 (height*5)+100]);
        axesH = axes;
        set(axesH,'Units','pixels','Position',[50,50,width*5,height*5]);
        hold on;
        
        key = cell(1,length(data.rXes));
        colorct=1;
        
        for h=(1:length(data.rXes))
            if ~isempty(data.(data.rXes{h}).ypos.toDmatrix)
                if sPlot==2 
                    % convert units of pixels/frame to mm/sec
                    scaleFactor = speedConv * data.params.fr;
                else
                    scaleFactor = 1;
                end
                
                    areaBar(data.xspace,... 
                        data.(data.rXes{h}).(data.feat{sPlot}).avg * scaleFactor, ...
                        data.(data.rXes{h}).(data.feat{sPlot}).se * scaleFactor,...
                        data.graph.colors{colorct+1}, data.graph.colors{colorct+1},...
                        '-',0.1);
            colorct = colorct+2;
                    
            if colorct > length(data.graph.colors)
                colorct = 1;
            end
            
            end
            if ~isempty(data.(data.rXes{h}).ypos.toDmatrix)
                key{h} = strcat((data.rXes{h}), ' (N=',string(size(data.(data.rXes{h}).ypos.toDmatrix,2)),')');
            end
        end

        key = key(~cellfun(@isempty,key));   
        
        if sPlot == 1
            ylabel('Relative y position','fontsize',data.graph.labelFont);
            %xtickangle(90);
            ylim([0 1]);
            lgd = legend(key,'Location','best');
            legend boxoff;
            lgd.FontSize = data.graph.legFont;
        else
            ylabel('Speed (mm/s)','fontsize',data.graph.labelFont);
            %ylim([0 4]);
            
            %ylim([0 1.4]);
            %set(gca,'ytick', 0:0.2:1.4);
            
            %xtickangle(90);
        end
        set(gca,'xtick', 1:3600*data.params.fr*data.graph.xInt:data.params.lastFrames+1);
        set(gca,'xticklabel', data.params.hoursBeforeDeath:-1*data.graph.xInt:0, 'fontsize', data.graph.tickFont,'TickLength',[0 0]);
        xlim([1 data.params.lastFrames+1]);
        xlabel('Hour prior to death','fontsize',data.graph.labelFont);
        
%         set(gcf,'PaperUnits','inches','PaperPosition',[0 0 6.991 2.1365]);
        print(strcat(outDir,data.filePre,'_',data.feat{sPlot},'_triggered on death.png'),'-dpng');
        print(strcat(outDir,data.filePre,'_',data.feat{sPlot},'_triggered on death.pdf'),'-dpdf');
        close();      
    end   
end