2016-10-24 2 views
1

ローディングラベルの位置を調整して、矢印の上に落ちないようにしたいと考えています。しかし、調整をどこにする必要があるのか​​分かりません。 geom_textはサイトの位置の調整に使用できますが、ベクトルが格納されている場所はstr(g)にはありません。autoplot - ラベルの読み込みを調整するにはどうすればよいですか?

library(ggplot2) 
library(ggfortify) 
df <- data.frame(replicate(10,sample(-10:10,10,rep=TRUE))) 
names(df) <- c('up','down','left','right','circle','square','triangle','x','r1','l1') 
rownames(df) <- paste('Dummy Site', seq(0,9,1)) 
g <- autoplot(prcomp(df[,-11], scale=TRUE), data=df, 
       loadings.label=TRUE, loadings=TRUE, 
       loadings.label.size=8, loadings.colour='blue', 
       label.size=5) + 
    geom_text(vjust=-1, label=rownames(df)) + 
    theme(plot.background=element_blank(), 
      panel.background=element_rect(fill='transparent',color='black',size=1), 
      legend.text=element_text(hjust=1), 
      legend.key=element_blank()) 
g 

私はggplot2::themeで見てきたと私はautoplotのヘルプドキュメントを検討しましたが、調整ラベル位置の一切の言及を見つけることができません。ボーナスポイントは矢印のベクトルに基づいて調整できますが、静的な調整は許容されます。 An example of what the above code generates

答えて

1

あなたはlayer_data(g, 2)で座標を取得することができます:

は現在、ここにプロットは次のようになります。しかしautoplot(prcomp.obj)ggbiplot()に他の引数を渡すので、loadings.label.hjust?ggbiplot参照)のように、ggbiplot()の引数を使用してlabelloadings.labelの位置を変更できます。

例コード:
arrow_ends <- layer_data(g, 2)[,c(2,4)] 

autoplot(prcomp(df[,-11], scale=TRUE), data=df, 
     loadings.label=TRUE, loadings=TRUE, 
     loadings.label.size=8, loadings.colour='blue', 
     label.size=5, loadings.label.vjust = 1.2) +  # change loadings.label position 
    geom_point(data = arrow_ends, aes(xend, yend), size = 3) + # the coordinates from layer_data(...) 
    geom_text(vjust=-1, label=rownames(df)) + 
    theme(plot.background=element_blank(), 
      panel.background=element_rect(fill='transparent',color='black',size=1), 
      legend.text=element_text(hjust=1), 
      legend.key=element_blank()) 

enter image description here

関連する問題