# pca with more than one grouping variable

3 views (last 30 days)
Kim Arnold on 14 Dec 2021
Commented: Jeff Miller on 16 Dec 2021
I want to do a pca plot of 20 samples I measured. For this I used gscatter. However, I want
1) to color my samples with 7 colors for the different type of organisms i measured and in addition
2) I want to give symbols for another variable called, dead or alive where I want to use square for dead and triangles for alive
How can I create a pca plot showing what I described above? do I need to use something else than gscatter?

Jeff Miller on 14 Dec 2021
gscatter will accept multiple grouping variables, in your case organism & dead/alive. There is an example in the gscatter documentation--search for "Scatter Plot with Multiple Grouping Variables". I believe you specify 14 colors and 14 symbols for your 7*2=14 combinations of the two grouping variables.
Kim Arnold on 16 Dec 2021
Edited: Kim Arnold on 16 Dec 2021
Dear jeff,
no worries, you were clear enough. I tried already what you suggested but this did not work correctly.
I finally figured out how it works. If you have like 17 samples and you have 6 groups (my mistake i wrote 7 earlier, but does not really matter regarding this aspect) of organisms which you want to color with 6 colors and then give in addition the symbols dead o and alive * you need first to know if you have in each of the 6 groups dead and alive or only dead or only alive because this makes a difference for the code and is something i did not mention to you when I described my problem. What I did first is the labels of the grouping and then I created
a logical array where 0= dead and 1=alive as [0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0].
Then I merged those two grouping variables as described in the example. Next you define
6 colors for your groups as 'rkkgbcmy'
Then this was the part which was first not clear to me. I went through my 6 groups. first group I had only dead --> symbol o, 2nd group I had dead and alive --> symbols o*, 3rd group only dead--> symbol o, 4th group only alive--> symbol *, 5th group only dead --> symbol o and 6th only dead --> symbol o which gives you an entry for 7 symbols (not 6!) because only in one group you have dead alive and need to specify 2 symbols.
'oo*o*oo'
However, it took a while to figure out, thanks for your input :)
Best,
Kim
Jeff Miller on 16 Dec 2021
Hi Kim,
You are welcome. That does sound pretty complicated, so extra-good that you got it working.