1

I'm trying to make a PCA plot for publication. That means without colours. However, all packages I have tried color the plot in the moment that you tell it to group the categories of the data.

I have already tried the packages: ggbiplot, ggfortify and factoextra. Unfortunaly, with no success.

On code that I have tried (from here):

library(ggbiplot)
mtcars.pca <- prcomp(mtcars[,c(1:7,10,11)], center = TRUE,scale. = TRUE)

ggbiplot(mtcars.pca)

ggbiplot(mtcars.pca, labels=rownames(mtcars))

mtcars.country <- c(rep("Japan", 3), rep("US",4), rep("Europe", 7),rep("US",3), "Europe", rep("Japan", 3), rep("US",4), rep("Europe", 3), "US", rep("Europe", 3))

ggbiplot(mtcars.pca,ellipse=TRUE,  labels=rownames(mtcars), groups=mtcars.country)

And return This image

But I what I need is something: like this

Hope I was clear enough.

2 Answers2

2

The answer of @SantiagoCapobianco has all of the pieces but does not put them together.

Using dataEllipse from the car package goes all of the way with the right arguments.

library(car)

mtcars.country <- factor(mtcars.country)

dataEllipse(mtcars.pca$x[,1], mtcars.pca$x[,2], mtcars.country, 
    levels=0.8, xlim=c(-5,5), ylim=c(-4,4), center.pch=0,
    col=rep("black", 3), pch=15:17)
legend("topleft", legend=levels(mtcars.country), pch=15:17, bty='n')

Data Ellipses

G5W
  • 36,531
  • 10
  • 47
  • 80
1

This could be half of the answer, as it lacks the ellipses:

data("mtcars")

mtcars.pca <- prcomp(mtcars[, c(1:7, 10, 11)], center = TRUE, scale = TRUE)
mtcars$country <- as.factor(c(rep("Japan", 3), rep("US",4), rep("Europe", 7),rep("US",3), "Europe", rep("Japan", 3), rep("US",4), rep("Europe", 3), "US", rep("Europe", 3)))

plot(mtcars.pca$x, pch = as.numeric(mtcars$country))
legend("topright", legend = c("Japan", "US", "Europe"), pch = 1:3)

Results in: enter image description here

To plot the ellipses, you can use the car package:

library(car)
dataEllipse(mtcars.pca$x[, 1:2], groups = mtcars$country, levels = 0.98, add = TRUE)

enter image description here

Credit to this question: Drawing ellipse in R

  • The OP explicitly asks for ellipses *without colour*. To do that you will have to use argument `col = rep(1, nrow(mtcars))` in the call to `dataEllipse`. And to draw the legend *after* the ellipses would be nicer. – Rui Barradas Mar 19 '19 at 18:46
  • @RuiBarradas Thanks for the comment. I didnt take all the time to realy post a good answer. Luckly G5W did it nice. I also didnt chage the colors of the ellipses because, when printed in black and white, the colors wouldnt matter. – Santiago Capobianco Mar 19 '19 at 20:22