【问题标题】:autoplot - How to adjust loading labels?autoplot - 如何调整加载标签?
【发布时间】:2016-10-24 16:28:47
【问题描述】:

我希望能够调整加载标签的位置,使它们不会落在箭头上方。但是,我不知道需要在哪里进行调整。 geom_text可以用来调整站点位置的位置,但是我在str(g)中找不到向量的存储位置。

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 的帮助文档,但找不到任何关于调整标签位置的信息。如果可以根据箭头的矢量进行调整,则可以加分,但可以接受静态调整。

目前,情节如下所示:

【问题讨论】:

    标签: r ggplot2 ggfortify


    【解决方案1】:

    您可以通过layer_data(g, 2)获取坐标。但是autoplot(prcomp.obj) 将其他参数传递给ggbiplot(),因此您可以使用ggbiplot() 的参数更改labelloadings.label 的位置,例如loadings.label.hjust(参见?ggbiplot)。

    示例代码:
    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()) 
    

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      相关资源
      最近更新 更多