1

I have created a PCA plot in using the ggbiplot() function, form the ggbiplot package, which is built on top of ggplot2. Here is an analogous, reproducible example:

library(ggbiplot)
data(wine)
wine.pca <- prcomp(wine, scale. = TRUE)
print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, ellipse = TRUE, circle = TRUE))

enter image description here So far so good. I want to change the legend order to grginolino, barbera, barolo (rather than barolo, grignolino, barbera as it is now). The names are stored in the environmental factor variable wine.class.

Apologies for such a simple question, but I cannot find a straight forward answer from the ggplot2 help that generalizes to this case.

colin
  • 2,606
  • 4
  • 27
  • 57
  • 1
    Your code is not reproducible. Is your `wine` data coming from the package Factominer ? I so use `data(wine, package = "FactoMineR")`. Then the data needs preprocessing before PCA --> numeric data is needed. – cbo Sep 30 '19 at 13:20
  • 1
    @cbo apologies, I forgot to include the library call to `ggbiplot`, which has the `wine` dataset. – colin Sep 30 '19 at 13:39

1 Answers1

2

You need to reorder wine.class levels; look below:

library(ggbiplot)

data(wine, package = "ggbiplot")
wine.pca <- prcomp(wine, scale. = TRUE)
wine.class.reorder <-  factor(wine.class, levels = c("grignolino", "barbera", "barolo"))

ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class.reorder, 
                   ellipse = TRUE, circle = TRUE)

M--
  • 25,431
  • 8
  • 61
  • 93