Translating values to gradient color scale
3 views (last 30 days)
Show older comments
I have a weighted PCA with 2 variables:
[wcoeff,score,~,~,~] = pca(vars,'VariableWeights','variance');
coefforth = inv(diag(std(vars)))* wcoeff;
figure(1)
biplot(coefforth(:,1:2),'scores',score(:,1:2));
How can I "translate" the values of "Component 1" (just one axis) to a color gradient map that I could use to color code another plot?
For example: the right-most datapoints in "Component 1" as blue and a color gradient to the left-most datapoints of "Component 1" in red.
Also, the PCA spreads the datapoints in a perpendicular angle, is there a way to spread the data in a 180 degree angle? In other words, is there a way to set the direction?
0 Comments
Answers (1)
Ayush Aniket
on 27 Dec 2024
To translate the values of "Component 1" from your PCA results into a color gradient map and use it for color-coding another plot, you can normalize the scores of "Component 1" to a range suitable for color mapping (e.g., 0 to 1). Then you can use a colormap to map normalized scores to colors and use these colors to color-code another plot as shown below:
% Perform weighted PCA
[wcoeff, score, ~, ~, ~] = pca(vars, 'VariableWeights', 'variance');
% Normalize Component 1 scores to range [0, 1]
component1Scores = score(:, 1);
normalizedScores = (component1Scores - min(component1Scores)) / (max(component1Scores) - min(component1Scores));
% Generate a colormap (e.g., 'jet' or 'parula')
cmap = jet(256); % You can choose any colormap
colorIndices = round(normalizedScores * (size(cmap, 1) - 1)) + 1;
colors = cmap(colorIndices, :);
% Plot using the color map
figure;
scatter(vars(:, 1), vars(:, 2), 50, colors, 'filled');
Additionally, to control the direction of the PCA components, you can manually adjust the sign of the principal component scores or coefficients. If you want to flip "Component 1" to reverse the direction:
% Flip the direction of Component 1
score(:, 1) = -score(:, 1);
This adjustment will effectively flip the direction of the component, spreading the data in the opposite direction along that axis. Since, PCA components are inherently sign-ambiguous, flipping them is valid if it suits your visualization needs.
0 Comments
See Also
Categories
Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!