[R] second legend in biplot

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Thu Dec 10 09:47:27 CET 2020


Thank you Jim

I am almost sure that biplot and prcomp itself cannot do point (text) coding 
according to some factor.

The closest way to what I want to achieve is to use result from prcomp and add 
points into existing biplot, something like that

PCA <- prcomp(iris[,-5], scale=T, center=T)
biplot(PCA)
points(PCA$x[,1:2], pch=20, col=iris$Species)
>
But maybe tweeking the code from fviz_pca_biplot could be easier as it is 
possible to have separate legend for point colour and shape but shape and 
colour is the same.
https://stackoverflow.com/questions/47482879/how-to-make-a-pretty-biplot-in-r-without-using-external-packages

I want shape and colour coded by different factors.

Anyway, thanks for your effort.

Cheers
Petr

> -----Original Message-----
> From: Jim Lemon <drjimlemon using gmail.com>
> Sent: Wednesday, December 9, 2020 11:37 PM
> To: PIKAL Petr <petr.pikal using precheza.cz>
> Cc: R mailing list <r-help using r-project.org>
> Subject: Re: [R] second legend in biplot
>
> Hi Petr,
> Perhaps legendg in plotrix can help:
>
> legendg(-180,600,c("State","Crime"),lty=1,col=list("black","red"),pch=3:4)
>
> Jim
>
> On Wed, Dec 9, 2020 at 10:31 PM PIKAL Petr <petr.pikal using precheza.cz>
> wrote:
> >
> > Thank you Jim.
> >
> > biplot can have distinct colours of points and arrows but not points
> > coded according to some factor. In simple example I provided, points
> > are coded according by 2 factors. What I would like to achieve is to
> > colour points and have their shapes coded by second factor, which
> > fviz_pca_biplot does, however it does not present this second factor in 
> > the
> legend.
> >
> > Probably the last resort is to rewrite original code which I would
> > like to avoid as I am not so experienced in grid graphics.
> >
> > Best regards
> > Petr
> >
> > > -----Original Message-----
> > > From: Jim Lemon <drjimlemon using gmail.com>
> > > Sent: Monday, December 7, 2020 9:53 PM
> > > To: PIKAL Petr <petr.pikal using precheza.cz>
> > > Cc: R mailing list <r-help using r-project.org>
> > > Subject: Re: [R] second legend in biplot
> > >
> > > Hi Petr,
> > > Here's an attempt, using the example in biplot.princomp:
> > >
> > > biplot(princomp(USArrests))
> > > > par("usr")
> > > [1] -497.2263  624.8856 -497.2263  624.8856
> > > legend(-180,600,c("State","Crime"),lty=1,col=c("black","red"))
> > >
> > > Jim
> > >
> > > On Mon, Dec 7, 2020 at 6:23 PM PIKAL Petr <petr.pikal using precheza.cz>
> wrote:
> > > >
> > > > Dear all
> > > >
> > > > I try to make fviz_pca_biplot with 2 (or more) legends. Below is
> > > > data and the code, which gives one legend (colour) for coating
> > > > variable and correctly shows triangles and circles for size
> > > > variable. But this is not shown in the legend. Hopefully somebody 
> > > > could
> help.
> > > >
> > > > And before you ask, I tried to contact maintainer about a week ago
> > > > but did not get response, therefore I try to post again here.
> > > >
> > > > Even an answer that current version of fviz_pca_biplot does not
> > > > support 2 legends with different point shapes is OK as it will
> > > > send me either to adapt source code or to stop trying impossible.
> > > >
> > > > Best regards
> > > > 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))
> > > >
> > > > ______________________________________________
> > > > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > > PLEASE do read the posting guide
> > > > http://www.R-project.org/posting-guide.html
> > > > and provide commented, minimal, self-contained, reproducible code.


More information about the R-help mailing list