Using col.hab for colorizing individuals

221 views
Skip to first unread message

Mahmood Naderan

unread,
Feb 5, 2021, 3:31:58 PM2/5/21
to factomin...@googlegroups.com
Hi
In the manual [1] page 62, I see a col.hab which is a vector of colors
for individuals.
However, I didn't find out how to use that. When I use

plot(res.pca,axes=c(1,2),col.hab="black black black red red red")

for 6 individuals, I see all points as black.
What is the correct syntax?

[1] https://cran.r-project.org/web/packages/FactoMineR/FactoMineR.pdf

Regards,
Mahmood

J.C. Deroubaix

unread,
Feb 5, 2021, 7:05:04 PM2/5/21
to factomin...@googlegroups.com
maybe : c(« black" ,« black" ,« black », « red » ,« red" « red")

Mahmood Naderan

unread,
Feb 6, 2021, 2:35:51 AM2/6/21
to factomin...@googlegroups.com
> maybe : c(« black" ,« black" ,« black », « red » ,« red" « red")

What is that '«'?
It is not a recognized character.
> plot(res.pca,axes=c(1,2),col.hab=c(« black" ,« black" ,« black »,
« red » ,« red" « red"))
Error: unexpected input in "plot(res.pca,axes=c(1,2),col.hab=c(▒"


Also the following command has no effect and all are black.
> plot(res.pca,axes=c(1,2),col.hab=c("black","black","black","red","red","red"))


Regards,
Mahmood

Womble

unread,
Feb 6, 2021, 12:33:35 PM2/6/21
to FactoMineR users
If you want to specify the colour of each individual point manually use the col.ind option:

plot(res.pca, axes=c(1,2), col.ind=c("black","black","black","red","red","red","red"))

To colour groups automatically when you have lots of individuals, set up your data with a column containing a factor.  So, if your data has five columns, create a sixth one with group membership.  Then:

res.pca<-PCA(mydata,quali.sup=6)
plot(res.pca,choix='ind',habillage=6,col.hab=c("black","red"))

Mahmood Naderan

unread,
Feb 7, 2021, 4:53:11 AM2/7/21
to factomin...@googlegroups.com
>If you want to specify the colour of each individual point manually use the col.ind option:
>
>plot(res.pca, axes=c(1,2), col.ind=c("black","black","black","red","red","red","red"))



Hi,
Unfortunately, that doesn't work

> library(FactoMineR)
> mydata <- read.csv('test.csv', header=T,row.names=1)
> res.pca = PCA(mydata[,1:4], scale.unit=TRUE, graph=F)
> head(mydata)
V1 V2 V3 V4
P1 73.6 0.7 74.6 3.1
P2 75.2 0.7 75.8 2.8
P3 6.5 0.0 7.3 2.5
P4 41.4 0.3 39.2 8.9
P5 5.4 0.1 18.2 1.1
P6 18.8 0.3 30.3 7.3
> plot(res.pca,axes=c(1,2),col.ind=c("black","black","black","red","red","red"))
Error: Aesthetics must be either length 1 or the same as the data (6): colour
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/rlang_error>
Aesthetics must be either length 1 or the same as the data (6): colour
Backtrace:
1. (function (x, ...) ...
2. ggplot2:::print.ggplot(x)
4. ggplot2:::ggplot_build.ggplot(x)
5. ggplot2:::by_layer(function(l, d) l$compute_geom_2(d))
6. ggplot2:::f(l = layers[[i]], d = data[[i]])
7. l$compute_geom_2(d)
8. ggplot2:::f(..., self = self)
9. self$geom$use_defaults(data, self$aes_params, modifiers)



There are P1~P6 and six colors in the vector. So, I wonder why it says
they have different sizes.
I also thought ath maybe it considers the first row which are V1~V4, I
created 7 colors as you mentioned.

> plot(res.pca,axes=c(1,2),col.ind=c("black","black","black","red","red","red","red"))
Error: Aesthetics must be either length 1 or the same as the data (6): colour
Run `rlang::last_error()` to see where the error occurred.


Regards,
Mahmood

Kris Lockyear

unread,
Feb 7, 2021, 7:59:28 AM2/7/21
to factomin...@googlegroups.com
This is a bug with the new version that uses ggplot to generate graphs.

Use: 

plot(res.pca,col.ind=c("black","black","black","red","red","red"),graph.type="classic")

and it will work.

K.


From: factomin...@googlegroups.com <factomin...@googlegroups.com> on behalf of Mahmood Naderan <mahmo...@gmail.com>
Sent: 07 February 2021 09:52
To: factomin...@googlegroups.com <factomin...@googlegroups.com>
Subject: Re: Using col.hab for colorizing individuals
 
--
Vous recevez ce message car vous êtes abonné à un sujet dans le groupe Google Groupes "FactoMineR users".
Pour vous désabonner de ce sujet, visitez le site https://groups.google.com/d/topic/factominer-users/7WKttFNNQPA/unsubscribe.
Pour vous désabonner de ce groupe et de tous ses sujets, envoyez un e-mail à l'adresse factominer-use...@googlegroups.com.
Cette discussion peut être lue sur le Web à l'adresse https://groups.google.com/d/msgid/factominer-users/CADa2P2Wy%2B85xnzinTLLURBuykX65aBnNwgaAMu-BRqP4yf9MDw%40mail.gmail.com.

Womble

unread,
Feb 7, 2021, 8:02:46 AM2/7/21
to FactoMineR users
PS  I much prefer the classic graphs anyway.  ggplot (does that stand for ghastly gimmicky plot?) has so much needless chart junk.  You can turn the junk off, but it is a real struggle to get a nice clean graph that I have long since given up on it.

Mahmood Naderan

unread,
Feb 7, 2021, 8:06:55 AM2/7/21
to factomin...@googlegroups.com
> plot(res.pca,col.ind=c("black","black","black","red","red","red"),graph.type="classic")

Yes that works. Thanks.

I also tried to use habillage and added a fifth column for the
categories. I tried to use quali.sup similar to the decathlon example,
but it fails


> mydata <- read.csv('test.csv', header=T,row.names=1)
> head(mydata)
V1 V2 V3 V4 CTG
P1 73.6 0.7 74.6 3.1 1
P2 75.2 0.7 75.8 2.8 1
P3 6.5 0.0 7.3 2.5 2
P4 41.4 0.3 39.2 8.9 2
P5 5.4 0.1 18.2 1.1 2
P6 18.8 0.3 30.3 7.3 3
> res.pca = PCA(mydata[,1:4], scale.unit=TRUE, quali.sup=5, graph=F)
Error in `[.data.frame`(Xtot, , quali.sup, drop = FALSE) :
undefined columns selected

So, I am not able to use

plot(res.pca,axes=c(1,2),habillage=5)


Any idea?


Regards,
Mahmood

Kris Lockyear

unread,
Feb 7, 2021, 8:21:07 AM2/7/21
to factomin...@googlegroups.com
Firstly, you need to convert your column CTG to a factor.

mydata$CTG <- as.factor(mydata$CTG)

Then your PCA command is incorrect:

pca.res<-PCA(mydata,graph=F,quali.sup=5)

Then you can run:

plot(pca.res,choix="ind",habillage=5)




Sent: 07 February 2021 13:06

To: factomin...@googlegroups.com <factomin...@googlegroups.com>
Subject: Re: Using col.hab for colorizing individuals
--
Vous recevez ce message car vous êtes abonné à un sujet dans le groupe Google Groupes "FactoMineR users".
Pour vous désabonner de ce sujet, visitez le site https://groups.google.com/d/topic/factominer-users/7WKttFNNQPA/unsubscribe.
Pour vous désabonner de ce groupe et de tous ses sujets, envoyez un e-mail à l'adresse factominer-use...@googlegroups.com.

Mahmood Naderan

unread,
Feb 7, 2021, 8:50:08 AM2/7/21
to factomin...@googlegroups.com
Well I did that, but in the plot, I see CTG_1, CTG_2 and CTG_3 in the plane.
The legend is fine, but I don't know how to remove the CTG_1~CTG_3.

> library(FactoMineR)

> mydata <- read.csv('test.csv', header=T,row.names=1)
> head(mydata)
     V1  V2   V3  V4 CTG
P1 73.6 0.7 74.6 3.1   1
P2 75.2 0.7 75.8 2.8   1
P3  6.5 0.0  7.3 2.5   2
P4 41.4 0.3 39.2 8.9   2
P5  5.4 0.1 18.2 1.1   2
P6 18.8 0.3 30.3 7.3   3
> mydata$CTG <- as.factor(mydata$CTG)
> pca.res<-PCA(mydata,graph=F,scale.unit=T,quali.sup=5)
> plot(pca.res,axes=c(1,2),choix="ind",habillage=5)


The output is shown at https://pasteboard.co/JNg3gPs.png


Regards,
Mahmood


Regards,
Mahmood



Kris Lockyear

unread,
Feb 7, 2021, 8:57:29 AM2/7/21
to factomin...@googlegroups.com
plot(mmPCA,choix="ind",habillage=5,invisible=c("quali"))


Sent: 07 February 2021 13:49

To: factomin...@googlegroups.com <factomin...@googlegroups.com>
Subject: Re: Using col.hab for colorizing individuals
--
Vous recevez ce message, car vous êtes abonné à un sujet dans le groupe Google Groupes "FactoMineR users".

Pour vous désabonner de ce sujet, visitez le site https://groups.google.com/d/topic/factominer-users/7WKttFNNQPA/unsubscribe.
Pour vous désabonner de ce groupe et de tous ses sujets, envoyez un e-mail à l'adresse factominer-use...@googlegroups.com.

Mahmood Naderan

unread,
Feb 7, 2021, 9:31:21 AM2/7/21
to factomin...@googlegroups.com
> plot(mmPCA,choix="ind",habillage=5,invisible=c("quali"))

No that seems to be wrong.



> library(FactoMineR)
> mydata <- read.csv('test.csv', header=T,row.names=1)
> head(mydata)
V1 V2 V3 V4 CTG
P1 73.6 0.7 74.6 3.1 1
P2 75.2 0.7 75.8 2.8 1
P3 6.5 0.0 7.3 2.5 2
P4 41.4 0.3 39.2 8.9 2
P5 5.4 0.1 18.2 1.1 2
P6 18.8 0.3 30.3 7.3 3
> mydata$CTG <- as.factor(mydata$CTG)
> pca.res<-PCA(mydata,graph=F,scale.unit=T,quali.sup=5)
> plot(res.pca,axes=c(1,2),choix="ind",habillage=5,invisible=c("quali"))
Error in if (habillage[1] == "none") { :
missing value where TRUE/FALSE needed




Regards,
Mahmood

Kris Lockyear

unread,
Feb 7, 2021, 9:32:59 AM2/7/21
to factomin...@googlegroups.com
Did you change mmPCA to the name you are using for the PCA, i.e., pca.res ?

I had a different name for the results of the PCA.




Sent: 07 February 2021 14:31

To: factomin...@googlegroups.com <factomin...@googlegroups.com>
Subject: Re: Using col.hab for colorizing individuals
--
Vous recevez ce message car vous êtes abonné à un sujet dans le groupe Google Groupes "FactoMineR users".

Pour vous désabonner de ce sujet, visitez le site https://groups.google.com/d/topic/factominer-users/7WKttFNNQPA/unsubscribe.
Pour vous désabonner de ce groupe et de tous ses sujets, envoyez un e-mail à l'adresse factominer-use...@googlegroups.com.

Mahmood Naderan

unread,
Feb 7, 2021, 9:34:43 AM2/7/21
to factomin...@googlegroups.com
Yes, as you can see in my previous email, I wrote the command based on
my variable names, e.g. mydata and res.pca.
But it failed.

Regards,
Mahmood
> --
> Vous recevez ce message, car vous êtes abonné au groupe Google Groupes "FactoMineR users".
> Pour vous désabonner de ce groupe et ne plus recevoir d'e-mails le concernant, envoyez un e-mail à l'adresse factominer-use...@googlegroups.com.
> Cette discussion peut être lue sur le Web à l'adresse https://groups.google.com/d/msgid/factominer-users/DBAP195MB10044F962AC07AE8DCF96DD9D9B09%40DBAP195MB1004.EURP195.PROD.OUTLOOK.COM.

Kris Lockyear

unread,
Feb 7, 2021, 9:47:08 AM2/7/21
to factomin...@googlegroups.com
You have saved your results to pca.res and then using res.pca in your plot command.  I'm guessing that res.pca exists from an earlier run.



Sent: 07 February 2021 14:34

Mahmood Naderan

unread,
Feb 7, 2021, 10:21:37 AM2/7/21
to factomin...@googlegroups.com
You are right. Sorry for the confusion. I was working with multiple
things and wrongly used pca.res and res.pca.
Thanks for pointing that out. It is now working.

Regards,
Mahmood
> Cette discussion peut être lue sur le Web à l'adresse https://groups.google.com/d/msgid/factominer-users/DBAP195MB10040B8595F02179DEDC6C25D9B09%40DBAP195MB1004.EURP195.PROD.OUTLOOK.COM.
Reply all
Reply to author
Forward
0 new messages