plotPCAExpVarianceAndTwoPCs.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function plotPCAExpVarianceAndTwoPCs(pcVectors, variances, pcaCoords, groupInds, figDir, nameSuffix)
  2. hFig = createPrintFig(14 * [1 1]);
  3. nSubPlots = 4;
  4. hSubAx = createSquarishSubplotGrid(nSubPlots, [0.1 0.12]);
  5. varColors = brewermap(2, 'OrRd');
  6. axes(hSubAx(1));
  7. bar(cumsum(variances), 'EdgeColor', 'none', 'FaceColor', varColors(1, :));
  8. hold on;
  9. bar(variances, 'EdgeColor', 'none', 'FaceColor', varColors(2, :));
  10. title(gca, 'Explained variance (%)')
  11. xlabel('Principal components')
  12. axis square tight
  13. axes(hSubAx(2));
  14. pcColorMap = brewermap([], 'YlOrRd');
  15. % pcColorMap = flipud(parula);
  16. colors = linearlyMapArrayToColors(variances, pcColorMap);
  17. hPCLines = plot((0: size(pcVectors, 1) - 1) / 10, bsxfun(@plus, pcVectors(:, 1: 5), -max(pcVectors(:)) * (1: 5)));
  18. for iLine = 1: numel(hPCLines)
  19. hPCLines(iLine).Color = colors(iLine, :);
  20. end
  21. hSubAx(2).YAxis.Visible = 'off';
  22. xlabel('Time (s)')
  23. title('Principal components')
  24. axis square tight
  25. axes(hSubAx(3));
  26. % Get PCA coords.
  27. colorMap = brewermap(256, 'YlGnBu');
  28. colorMap = flipud(colorMap);
  29. % colorMap = colorMap(1: floor(0.7*size(colorMap, 1)), :);
  30. colormap(colorMap);
  31. hScatt = scatter(pcaCoords(:, 1), pcaCoords(:, 2), [], groupInds, 'filled', ...
  32. 'MArkerFaceAlpha', 1, 'MarkerEdgeColor', 'k');
  33. xlabel(['First PC (' num2str(variances(1), 2) '%)'])
  34. ylabel(['Second PC (' num2str(variances(2), 2) '%)'])
  35. title('Principal components 1 vs 2')
  36. axis square
  37. axes(hSubAx(4));
  38. colormap(colorMap)
  39. hScatt = scatter(pcaCoords(:, 1), pcaCoords(:, 3), [], groupInds, 'filled', ...
  40. 'MArkerFaceAlpha', 1, 'MarkerEdgeColor', 'k');
  41. xlabel(['First PC (' num2str(variances(1), 2) '%)'])
  42. ylabel(['Third PC (' num2str(variances(3), 2) '%)'])
  43. title('Principal components 1 vs 3')
  44. originPos = hSubAx(4).Position;
  45. hCbar = colorbar;
  46. hSubAx(4).Position = originPos;
  47. axis square
  48. arrayfun(@prettifyAxes, hSubAx);
  49. arrayfun(@offsetAxes, hSubAx);
  50. setFontForThesis(hSubAx, gcf)
  51. figFileName = ['OnOff-PCA' nameSuffix];
  52. % set(gcf,'renderer','painters')
  53. % set(gcf, 'PaperPositionMode', 'auto');
  54. print(hFig, [figDir figFileName '.pdf'], '-dpdf')
  55. print(hFig, [figDir figFileName '.png'], '-dpng', '-r300')
  56. end