prcomp 之后 ggplot2 和 autoplot() 的区别?

发布于 2025-01-09 11:28:13 字数 1028 浏览 0 评论 0原文

我用 autoplot() 制作了一个 PCA 图,但我想只在其中 2 个组周围有椭圆,而不是所有 3 个组。因此我切换到 ggplot。但是,我的轴在 autoplot 和 ggplot 方法之间似乎有所不同。看看 p1 和 p2 之间的区别:

library(ggplot2)
library(ggfortify)
library(tidyr)

x <- iris[1:4]
pc <- prcomp(x)
df <- cbind(pc$x[,1:2], iris[,5]) %>% as.data.frame()
df$PC1 <- as.numeric(df$PC1)
df$PC2 <- as.numeric(df$PC2)
df$V3 <- as.factor(df$V3)

#ggplot method
p1 <- ggplot(df, aes(PC1, PC2, colour = V3)) +
  geom_point(size = 3, aes(shape = V3)) +
  stat_ellipse(geom = "polygon", aes(fill = after_scale(alpha(colour, 0))),
               data = df[df$V3 == "1" | df$V3 == "2",], size = 1) 
p1



#autoplot method
y <- prcomp(x)
x2 <- as.data.frame(cbind(x, iris[,5]))
x2$`iris[, 5]` <- as.factor(x2$`iris[, 5]`)

p2<- autoplot(y, 
              data = x2, 
              colour = 'iris[, 5]', 
              label = F, 
              shape = 'iris[, 5]',
              size = 2)

p2


Created on 2022-02-22 by the reprex package (v2.0.1)

为什么我得到不同的轴?

I made a PCA plot with autoplot(), but I wanted to have ellipses around only 2 of the groups instead of all 3. Therefore I switched to ggplot. However, it seems that my axes are different between autoplot and ggplot methods. Look at the difference between p1 and p2:

library(ggplot2)
library(ggfortify)
library(tidyr)

x <- iris[1:4]
pc <- prcomp(x)
df <- cbind(pc$x[,1:2], iris[,5]) %>% as.data.frame()
df$PC1 <- as.numeric(df$PC1)
df$PC2 <- as.numeric(df$PC2)
df$V3 <- as.factor(df$V3)

#ggplot method
p1 <- ggplot(df, aes(PC1, PC2, colour = V3)) +
  geom_point(size = 3, aes(shape = V3)) +
  stat_ellipse(geom = "polygon", aes(fill = after_scale(alpha(colour, 0))),
               data = df[df$V3 == "1" | df$V3 == "2",], size = 1) 
p1



#autoplot method
y <- prcomp(x)
x2 <- as.data.frame(cbind(x, iris[,5]))
x2

I made a PCA plot with autoplot(), but I wanted to have ellipses around only 2 of the groups instead of all 3. Therefore I switched to ggplot. However, it seems that my axes are different between autoplot and ggplot methods. Look at the difference between p1 and p2:

iris[, 5]` <- as.factor(x2

I made a PCA plot with autoplot(), but I wanted to have ellipses around only 2 of the groups instead of all 3. Therefore I switched to ggplot. However, it seems that my axes are different between autoplot and ggplot methods. Look at the difference between p1 and p2:

iris[, 5]`) p2<- autoplot(y, data = x2, colour = 'iris[, 5]', label = F, shape = 'iris[, 5]', size = 2) p2 Created on 2022-02-22 by the reprex package (v2.0.1)

Why do I get different axes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2025-01-16 11:28:13

在自动绘图方法中,主成分被缩放,因此要获得相同的结果,您可以这样做:

x <- iris[1:4]
pc <- prcomp(x)
df <- cbind(pc$x[,1:2], iris[,5]) %>% as.data.frame()
df$PC1 <- as.numeric(df$PC1) / (pc$sdev[1] * sqrt(nrow(iris)))
df$PC2 <- as.numeric(df$PC2) / (pc$sdev[2] * sqrt(nrow(iris)))
df$V3 <- as.factor(df$V3)

#ggplot method
p1 <- ggplot(df, aes(PC1, PC2, colour = V3)) +
  geom_point(size = 3, aes(shape = V3)) +
  stat_ellipse(geom = "polygon", aes(fill = after_scale(alpha(colour, 0))),
               data = df[df$V3 == "1" | df$V3 == "2",], size = 1) 
p1

在此处输入图像描述

In the autoplot method, the principal components are scaled, so to get the same result you would do:

x <- iris[1:4]
pc <- prcomp(x)
df <- cbind(pc$x[,1:2], iris[,5]) %>% as.data.frame()
df$PC1 <- as.numeric(df$PC1) / (pc$sdev[1] * sqrt(nrow(iris)))
df$PC2 <- as.numeric(df$PC2) / (pc$sdev[2] * sqrt(nrow(iris)))
df$V3 <- as.factor(df$V3)

#ggplot method
p1 <- ggplot(df, aes(PC1, PC2, colour = V3)) +
  geom_point(size = 3, aes(shape = V3)) +
  stat_ellipse(geom = "polygon", aes(fill = after_scale(alpha(colour, 0))),
               data = df[df$V3 == "1" | df$V3 == "2",], size = 1) 
p1

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文