Coloring The Dots in biPlot Chart

I have created biplot as below and I'm looking for a way to distinguish the dots by different colors according to their group name. There are 12 groups and here are mydata and codes.
PC1andPC2.png
categories = ['F1';'F2';'F3';'F4';'F5';'F6';'F7';'F8'];
load('MAT_ALL.mat')
figure(1)
[coefforth,score,~,~,explainedVar] = pca(MaT_All(:,9:16));
load('DataGroup.mat')
clusters = DataGroup(:,20);
[coefforth,score,~,~,explainedVar] = pca(MaT_All(:,9:16));
figure(3)
biplot([coefforth(:,1) coefforth(:,2)],'Scores',[score(:,1) score(:,2)],'Varlabels',categories);

 Respuesta aceptada

Adam Danz
Adam Danz el 25 de Abr. de 2019
Editada: Adam Danz el 25 de Abr. de 2019
The biplot() function has an output that lists handles to all objects in the plot. All you need to do is isolate the handles to the scatter points by referencing the handle tags and then assign color based on the category.
If you have any questions, feel free to leave a comment.
% Your code
categories = ['F1';'F2';'F3';'F4';'F5';'F6';'F7';'F8'];
load('MAT_ALL.mat')
% figure(1) (No need for this)
[coefforth,score,~,~,explainedVar] = pca(MaT_All(:,9:16));
load('DataGroup.mat')
clusters = DataGroup(:,20);
[coefforth,score,~,~,explainedVar] = pca(MaT_All(:,9:16));
figure()
% Store handle to biplot
h = biplot([coefforth(:,1) coefforth(:,2)],'Scores',[score(:,1) score(:,2)],'Varlabels',categories);
% Identify each handle
hID = get(h, 'tag');
% Isolate handles to scatter points
hPt = h(strcmp(hID,'obsmarker'));
% Identify cluster groups
grp = findgroups(clusters); %r2015b or later - leave comment if you need an alternative
grp(isnan(grp)) = max(grp(~isnan(grp)))+1;
grpID = 1:max(grp);
% assign colors and legend display name
clrMap = lines(length(unique(grp))); % using 'lines' colormap
for i = 1:max(grp)
set(hPt(grp==i), 'Color', clrMap(i,:), 'DisplayName', sprintf('Cluster %d', grpID(i)))
end
% add legend to identify cluster
[~, unqIdx] = unique(grp);
legend(hPt(unqIdx))
You can select a different color map (I'm using 'lines'). : https://www.mathworks.com/help/matlab/ref/colormap.html#buc3wsn-1-map190425 094457-Figure 1.jpg

11 comentarios

Dear Adam
Thank you so much for writing this code. I totally appreciate for your help. You really made my day. I have a further question regarding this problem. I wanted to create scatter plot for PC1 and 2 scores while showing their clusters as below but it is not working. Is there any way to help on this please.
figure(2)
gscatter(score(:,1),score(:,2),clusters)
Adam Danz
Adam Danz el 25 de Abr. de 2019
Editada: Adam Danz el 25 de Abr. de 2019
Hi Yaser, I'm glad I could help.
The reason your line of code above isn't working is because 'clusters' is a table and gscatter() doesn't like tables.
You could use my grp variable instead or pull the data from the table.
figure(2)
gscatter(score(:,1),score(:,2),grp)
% OR
gscatter(score(:,1),score(:,2),clusters.Variables)
Yaser Khojah
Yaser Khojah el 25 de Abr. de 2019
Editada: Yaser Khojah el 25 de Abr. de 2019
Dear Adam,
Earlier I have tried something like the below and it worked. Please note cul and clusters have the same formate.
[clu, centroid] = kmeans(score(:,1:2),12);
figure(2)
gscatter(score(:,1),score(:,2),clu)
Now I tried yous and it is working. I'm just wondering what is the different but grp and clusters since I see they have the same formate. Thank you so so so much
Adam Danz
Adam Danz el 25 de Abr. de 2019
Editada: Adam Danz el 25 de Abr. de 2019
Your variable "clusters" is a table that contains cluster number 1 to 11 and a bunch of NaN values. You mentioned that you have 12 clusters so I interpreted the NaNs as cluster #12.
My grp is a grouping variable of numbers 1 to 12 where 1:11 match your cluster 1:11 and the 12s represent the NaNs in 'clusters'.
I recommend going through every line of my code to make sure it's doing what you want it to do. There's only 7-8 lines to look at. If you have any questions, feel free to ask.
Yaser Khojah
Yaser Khojah el 26 de Abr. de 2019
Dear Adam, thank you for all your help. It is working perfect as I want it. What is your recommendation since I have a lot of data and sometimes the computer freezes when I like to change something manually such as the marker size?
Adam Danz
Adam Danz el 26 de Abr. de 2019
I know what that frustration feels like and I wish I had a solution. Here are a few suggestions.
1) when you're developing algorithms, plots, etc, use a much smaller dataset - even if it's made of fake data. That way the trial-and-error process isn't impeded by loading and processing large data. When your code is perfected, then load the real data and test it.
2) (this one's obvious) Use a machine with more RAM or a better processor.
3) Search your code for inefficiencies. Using the profile() function, you can time the execution of your code and it can produce a report that lists execution time for each function.
4) Using remote desktop and a second monitor, often times I'll run matlab on two machines and go back and forth, working on whichever one isn't busy.
Yaser Khojah
Yaser Khojah el 26 de Abr. de 2019
Thanks for all these tips.
Adam Danz
Adam Danz el 17 de Nov. de 2020
Editada: Adam Danz el 17 de Nov. de 2020
mohammed alenazy asked: "How to make dots circles instead? thanks!" (comment deleted)
My response:
h = bipolot(__);
set(h, 'Marker', 'o')
Hi Adam, your suggestion was very good.
In my case, the groping variables are not ordered and I can't find the way to assign them in the proper order.
Below just an example to show you what I mean.
Do you have any suggestion?
Thanks
clear; clc;
load iris.dat;
species = grp2idx(iris(:,5));
[n_row,n_col] = size(iris);
irisRandom = iris(randperm(size(iris, 1)), :);
[loadings,scores] = pca(irisRandom(:,1:3));
% uniqueVarieta = unique(species);
% Store handle to biplot
h = biplot(loadings(:,1:2),...
'Scores',scores(:,1:2));
% Identify each handle
hID = get(h, 'tag');
% Isolate handles to scatter points
hPt = h(strcmp(hID,'obsmarker'));
% Identify cluster groups
grp = findgroups(species);
grp(isnan(grp)) = max(grp(~isnan(grp)))+1;
grpID = 1:max(grp);
% assign colors and legend display name
%clrMap = lines(length(unique(grp))); % using 'lines' colormap
clrMap = lines(length(unique(species))); % using 'lines' colormap
for i = 1:max(grp)
set(hPt(grp==i), 'Color', clrMap(i,:), 'DisplayName', sprintf('species %d', grpID(i)))
end
% add legend to identify cluster
[~, unqIdx] = unique(grp);
legend(hPt(unqIdx))
You need to keep track of your random permutation indices and apply the same permutation to the species vector.
randIdx = randperm(size(iris, 1));
irisRandom = iris(randIdx, :);
species = species(randIdx);
ALESSANDRO D'ALESSANDRO
ALESSANDRO D'ALESSANDRO el 1 de Dic. de 2020
thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 25 de Abr. de 2019

Comentada:

el 1 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by