2017-06-23 44 views
2

我正在運行帶有varimax旋轉的主成分分析,並希望顯示看似簡單的情節,但是我的加載向量在某些地方非常接近,而且它們傾向於哪些因素的標籤傾向於重疊。這是ggrepel進來分離標籤的地方。我現在的困境是弄清楚如何連接兩者。我使用了自動添加所需文本的自動繪圖,並且很難定義要排斥哪些文本。可能還有其他方法可以解決這個問題,我願意提出建議。我有我的代碼,但有重疊和我的企圖排斥下面的代碼之一。使用ggfortify和ggrepel進行pca

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)))) 

enter image description here

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") 

回答

1

您可以使用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") 

enter image description here

隨着擊退標籤:

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") 

enter image description here

+1

非常感謝您J.康尼馬上解決了我的問題! –

0

您沒有提供任何數據,使這個可重複的,但是你可能有更多的運氣與包裝,ggbiplot

library(ggbiplot) 

data(mtcars) 

standardised<-as.data.frame(scale(mtcars[2:ncol(mtcars)])) 

mtcars.pca<-prcomp(standardised,retx=TRUE) 

ggbiplot(mtcars.pca, obs.scale=1, var.scale=1, ellipse=F, circle=F,labels.size = 4) 

enter image description here

+0

我有嘗試使用'ggbiplot',但是,我正在運行R版本3.4.1測試版並且它不可用。當我嘗試安裝軟件包時,我收到了響應:'> install.packages(「ggbiplot」) 將軟件包安裝到'C:/ Users/efn1/R庫' (因爲'lib'未指定) 安裝時的警告.packages: 包'ggbiplot'不可用(對於R版本3.4.1測試版)' –

+0

好吧。你能否在你的問題中提供'dput(built.df9)'的輸出以使其具有可重現性? –

+0

由於輸出太大而不適合該示例,因此我無法放入dput。我對重現的難度表示歉意,但我的數據集相當大。 –