MATLAB Answers

# Indexing scores to plot pca results in biplot

51 views (last 30 days)
Heidi Hirsh on 29 Sep 2020
Commented: Heidi Hirsh on 30 Sep 2020
I am trying to visualize the results of a principal component analysis using biplot. I want to color the data by site (or time intervals, etc). Let's take coloring the results by site as an example: In this case the first half of the data (first 4629 rows in the scores output from the pca function) is for site 1 and the second half for site 2 (rows 4630 to the end in the scores output).
When I plot all of the data in gray (circles) and the sites in colors (points), the first site (blue) doesn't line up with the gray points and I can't figure out why.
Am I indexing the score values correctly if score is a 9258x8 double and I want to plot the first 4629 rows (site 1) in blue then the second 4629 rows (site 2) in red?
X = horzcat(DO_s,O2sat_s,PH_s,Temp_s,Sal_s,Depth_s,PAR_s,absVel_s);
[coeff,score,latent,tsquared] = pca(X);
f4=figure(4)
hold on; box on;
biplot(coeff(:,1:2),'scores',score(:,1:2),'color','k','Marker','o','markeredgecolor',[.9 .9 .9])
biplot(coeff(:,1:2),'scores',score(1:4629,1:2),'color','k','Marker','.','markeredgecolor','b','varlabels',...
{'DO','O2sat','pH','Temp','Sal','Depth','PAR','|Velocity|'}); %Site 1 (1:4629)
biplot(coeff(:,1:2),'scores',score(4630:end,1:2),'color','k','Marker','.','markeredgecolor','r') %Site 2 (4630:end)
yline(0,'--')
xline(0,'--')
title('Full Deployment');
set(gca,'fontsize',18)
##### 2 CommentsShowHide 1 older comment
Heidi Hirsh on 30 Sep 2020
I think I did it!
My code now reads:
X = horzcat(DO_s,O2sat_s,PH_s,Temp_s,Sal_s,Depth_s,PAR_s,absVel_s);
[coeff,score,latent,tsquared,explained] = pca(X);
subgroup1 = repmat(1,length(time),1); %first 4629 (1:4629) points are MSP1
subgroup2 = repmat(2,length(time),1); %second 4629 (4630:end) are MSP2
site = vertcat(subgroup1,subgroup2);
close all
% %figure out how to color MSP1 versus MSP2
% color2 = [1 .6 .6]
% color1 = [.4 .6 1]
f4=figure(4) %scaled data
hold on; box on;
% biplot(coeff(:,1:2),'scores',score(:,1:2),'color','Marker','o','markeredgecolor',[.7 .7 .7]);
h= biplot(coeff(:,1:2),'scores',score(:,1:2),'color','k','varlabels',...
{'DO','O2sat','pH','Temp','Sal','Depth','PAR','|Velocity|'});
%color by site
hID = get(h,'tag'); %identify handle
hPt = h(strcmp(hID,'obsmarker')); %isolate handles to scatter points
grp = findgroups(site);
grpID = 1:max(grp);
clrMap = winter(length(unique(grp)));
for i = 1:max(grp)
set(hPt(grp==i), 'Color', clrMap(i,:), 'DisplayName', sprintf('MSP%d', grpID(i)))
end
yline(0,'--')
xline(0,'--')
title('Full Deployment');
set(gca,'fontsize',18)
%legend for color
[~, unqIdx] = unique(grp);
legend(hPt(unqIdx))

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!