我一直在努力在GGPLOT2/GGFORTIFY PCA中重新缩放负载(箭头)长度。 我已经广泛地环顾了一个答案,以及我找到的唯一信息的信息是新的Biplot函数,或者是指其他完全不同的PCA软件包(GGBIPLOT,FACTOEXTRA),我都不想解决我想回答的问题:

问题描述 投票:0回答:1

数据来自我正在参加的类中的在线文件,因此,如果您安装了GGPLOT2和GGFORTIFY软件包,则可以复制此文件。 下面的图 我希望GGPLOT看起来像

ggplot实际上看起来像

Edit: 在下面添加可重复的代码。
iris.res <- 
  iris %>% 
  select(Sepal.Length:Petal.Width) %>% 
  as.matrix(.) %>% 
  prcomp(., scale = F)

autoplot(iris.res, data = iris, size = 4, col = "Species", shape = "Species", 
         x = 1, y = 2, #components 1 and 2
         loadings = T, loadings.colour = "grey50", loadings.label = T, 
         loadings.label.colour = "grey50", loadings.label.repel = T) + #loadings are arrows
  geom_vline(xintercept = 0, lty = 2) +
  geom_hline(yintercept = 0, lty = 2) +
  theme(aspect.ratio = 1) +
  theme_bw()

这个答案可能是在OP需要之后很长时间,但是我提供了它,因为我已经在同一问题上摔跤了一段时间,也许我可以为其他人付出同样的努力。 # Load data iris <- data.frame(iris) # Do PCA PCA <- prcomp(iris[,1:4]) # Extract PC axes for plotting PCAvalues <- data.frame(Species = iris$Species, PCA$x) # Extract loadings of the variables PCAloadings <- data.frame(Variables = rownames(PCA$rotation), PCA$rotation) # Plot ggplot(PCAvalues, aes(x = PC1, y = PC2, colour = Species)) + geom_segment(data = PCAloadings, aes(x = 0, y = 0, xend = (PC1*5), yend = (PC2*5)), arrow = arrow(length = unit(1/2, "picas")), color = "black") + geom_point(size = 3) + annotate("text", x = (PCAloadings$PC1*5), y = (PCAloadings$PC2*5), label = PCAloadings$Variables)

为了增加箭头长度,将the the the the the the the and的加载倍增。有了一些试验和精力,可以弄清楚使用什么数字。

将标签放在正确的位置,将PC轴乘以
xend
调用中的相同值。
r ggplot2 pca ggfortify
1个回答
9
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.