您可以通过多种方式做到这一点:
- 在变量前面使用减号来反转
- 使用 scale_*_reverse()
这是一个最小示例,显示了在著名的 iris 数据集上的两个 PCA 上的这些操作。
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)