Skip to content

Customizing plot

4 messages · Mahmood Naderan-Tahan, Duncan Murdoch, PIKAL Petr

#
Hi,
1- How can I assign different  colors to different observations in the PCA chart? Assume, I have 10  observations and I want to use black color for the first 5 and read for  the next 5 observations.

Currently, I use 

?res.pca = PCA(mydata[,1:37], scale.unit=TRUE, graph=T)
?plot(res.pca,axes=c(1,2))


?
2- How can I change the plot area size and font sizes? With these commands
?
?dev.new(width=20, height=4)
?plot(res.pca,axes=c(1,2))

 The final plot is not what I expected. Please see the picture at?https://imgur.com/UINTnv7 
?


     
 
 Regards,
 Mahmood
#
On 05/02/2021 10:44 a.m., Mahmood Naderan-Tahan wrote:
No need to post twice.  You probably didn't get an answer the first time 
because it was just an hour ago, and you didn't include "mydata" (or a 
suitable subset) in your posting.  For the best chance at getting a 
helpful answer, post code that others can run.

Duncan Murdoch
#
I received a bounce reply with [[alternative HTML version deleted]] and thought it has been rejected...


So, here are the commands I use
???? 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
The test data and two plots are available at https://gofile.io/d/Qza0PR
As you can see the second plot is not properly sketched as I want to get a full size chart in the window.

For the colors, I want to use black for the first 3 and red for the next three observations. 


Regards,
Mahmood

    
From: Duncan Murdoch <murdoch.duncan at gmail.com>
Sent: Friday, February 5, 2021 5:01:44 PM
To: Mahmood Naderan-Tahan; r-help at r-project.org
Subject: Re: [R] Customizing plot
?
On 05/02/2021 10:44 a.m., Mahmood Naderan-Tahan wrote:
No need to post twice.? You probably didn't get an answer the first time 
because it was just an hour ago, and you didn't include "mydata" (or a 
suitable subset) in your posting.? For the best chance at getting a 
helpful answer, post code that others can run.

Duncan Murdoch
2 days later
#
Hallo Mahmood

I recently struggled with similar problem useing factoextra package. You
could fiddle with xlim and ylim to set size of plot and col.ind to set the
colour.

Another option is to use prcomp function to generate data and fine tune
biplot.

See parameters expand, xlim and ylim for biplot size and points to add
points with different shape, colour and/or size.

Below is example with factoextra

Cheers
Petr

library(factoextra)
library(FactoMineR)
 
  fit <- PCA(temp, quali.sup=c(9,10))
  fviz_pca_biplot(fit, col.ind = temp$coating, repel=T, col.var = 
  "black", palette = "lancet", invisible="quali", pointsize=5, 
  pointshape=temp$size, legend.title = list(col = "Coating", 
  shape="Size"), xlim=c(-6,6), title="Instillation results")
 
  temp <- structure(list(leukocyte28 = c(96875L, 73438L, 68229L, 
  94479L, 76563L, 141667L, 111042L, 93333L, 132083L, 103542L, 61667L, 
  77708L ),
  macrophage28 = c(60.29, 99.13, 97.04, 98.54, 98.46, 75.2, 89.71, 98, 
  82, 98.83, 99.08, 98.54), pmn28 = c(38.58, 0.58, 2.71, 0.92, 1, 
  24.25, 9.29, 1.5, 15.08, 0.92, 0.67, 1), lymphocyte28 = c(1.13, 
  0.29, 0.25, 0.54, 0.54, 0.55, 1, 0.5, 2.92, 0.25, 0.25, 0.46),
      leukocyte3 = c(186042L, 111250L, 114375L, 111146L, 98854L,
      156250L, 250625L, 183125L, 202917L, 161875L, 184792L, 128333L
      ), macrophage3 = c(53.88, 95.96, 98.29, 98.92, 98.92, 78.3,
      82.33, 97.83, 84.79, 97.25, 97.75, 98.46), pmn3 = c(44.75,
      3.46, 1.29, 0.67, 0.71, 20.4, 16.67, 1.92, 14.04, 1.92, 1.67,
      1.21), lymphocyte3 = c(1.38, 0.58, 0.42, 0.42, 0.38, 1.3,
      1, 0.25, 1.17, 0.83, 0.58, 0.33), coating = structure(c(3L,
      3L, 3L, 1L, 7L, 1L, 2L, 5L, 4L, 6L, 3L, 3L), .Label = c("alumina",
      "both", "none", "phosphate", "silica", "tungsten", "zirconia"
      ), class = "factor"), size = structure(c(1L, 1L, 2L, 2L,
      2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L), .Label = c("nano", "pigmentary"
      ), class = "factor")), class = "data.frame", row.names = c(NA,
  12L))
the