【问题标题】:PCA in ggplot - appearanceggplot 中的 PCA - 外观
【发布时间】:2021-08-27 11:49:13
【问题描述】:

我正在使用 ggplot2 创建两个 PCA 以证明它们看起来相同。但是,当我为两者运行相同的脚本时,它们看起来确实很相似,尽管是“倒置的”。我想知道如何通过更改轴使它们看起来相同。 我正在向你展示结果。

提前致谢

【问题讨论】:

  • 这似乎只是 PCA 的一个属性,如果该过程对相同的数据重复两次,PC 可以反转。也就是说,您可以使用scale_y_reverse()scale_x_reverse() 来反转轴。

标签: r ggplot2 pca


【解决方案1】:

您可以通过多种方式做到这一点:

  • 在变量前面使用减号来反转
  • 使用 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    相关资源
    最近更新 更多