Rita
Rita

Reputation: 13

PCA in ggplot - appearance

I'm using ggplot2 to create two PCAs to prove that they look the same. However, when I run the same script for both, they look similiar indeed, altough "inverted". I wonder how can I make them look the same, by changing the axis. I'm showing you the results.

enter image description here enter image description here

Thanks in advance

Upvotes: 0

Views: 716

Answers (1)

scrameri
scrameri

Reputation: 707

You can do this in several ways:

  • use a minus sign in front of the variable to reverse
  • use scale_*_reverse()

Here is a minimal example that shows these operations on two PCAs on the famous iris dataset.

data(iris)

# PCA
pca1 <- prcomp(iris[,1:4], center = T, scale. = T)
pca2 <- princomp(iris[,1:4], cor = T)

# plots
library(ggplot2)
p1 <- ggplot(data.frame(pca1$x, Species = iris$Species),
             aes(x= PC1, y = PC2, color = Species)) +
  geom_point() +
  theme_bw() +
  ggtitle("PCA on iris dataset using <prcomp>")

p2 <- ggplot(data.frame(pca2$scores, Species = iris$Species),
             aes(x = Comp.1, y = Comp.2, color = Species)) +
  geom_point() +
  theme_bw() +
  ggtitle("PCA on iris dataset using <princomp>")

p3 <- ggplot(data.frame(pca2$scores, Species = iris$Species),
             aes(x = Comp.1, y = -Comp.2, color = Species)) +
  geom_point() +
  theme_bw() +
  ggtitle("PCA using <princomp> - minus sign")

p4 <- ggplot(data.frame(pca2$scores, Species = iris$Species),
             aes(x = Comp.1, y = Comp.2, color = Species)) +
  geom_point() +
  scale_y_reverse() +
  theme_bw() +
  ggtitle("PCA using <princomp> - scale_y_reverse")

# ggpubr::ggarrange(p1,p2,p3,p4)

enter image description here

Upvotes: 1

Related Questions