为 pca 使用 ggfortify 和 ggrepel

E. *_*son 6 r ggplot2 ggfortify ggrepel

我正在使用方差极大旋转运行主成分分析,并希望显示看起来足够简单的图,但是我的加载向量在某些地方非常接近,并且它们倾向于重叠的因素的标签。这就是 ggrepel 用于分离标签的地方。我现在的困境是弄清楚如何将两者联系起来。我使用了自动绘图,它会自动添加所需的文本,并且很难定义要排斥的文本。可能还有其他解决方法,我愿意接受建议。我有我的代码有效但有重叠和我试图排斥下面的代码之一。

autoplot(prcomp(built.df9),
loadings = TRUE, loadings.colour = 'blue', loadings.label = TRUE, 
loadings.label.size = 4, loading.label.color = 'red') +
ggtitle(label = "Principal Component Analysis and Varimax Rotation for Built 
Environment Indicators") +
geom_text_repel(aes(label = rownames(prcomp(built.df9))))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

autoplot(prcomp(built.df9),
loadings = TRUE, loadings.colour = 'blue', loadings.label = TRUE, 
loadings.label.size = 4, loading.label.color = 'red') +
ggtitle(label = "Principal Component Analysis and Varimax Rotation for Built 
Environment Indicators")
Run Code Online (Sandbox Code Playgroud)

J.C*_*Con 8

您可以loadings.label.repel=Tggfortify包中使用。

此示例使用您的相同代码,仅使用mtcars数据集。

无排斥标签:

library(ggplot2)
library(ggfortify)

autoplot(prcomp(mtcars),
         loadings = TRUE, loadings.colour = 'blue', loadings.label = TRUE, 
         loadings.label.size = 4, loading.label.color = 'red') +
  ggtitle(label = "Principal Component Analysis and Varimax Rotation for Built 
          Environment Indicators") 
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

带有排斥标签:

autoplot(prcomp(mtcars),
         loadings = TRUE, loadings.colour = 'blue', loadings.label = TRUE, 
         loadings.label.size = 4, loading.label.color = 'red',loadings.label.repel=T) +
  ggtitle(label = "Principal Component Analysis and Varimax Rotation for Built 
          Environment Indicators") 
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明