代码之家  ›  专栏  ›  技术社区  ›  J.Con

调整ggplot对象R中的图例标题位置

  •  2
  • J.Con  · 技术社区  · 8 年前

    在下面的示例中,我使用 factoextra 和 FactoMineR 创建一个双环图这个情节有一个彩色条,标题技术上是居中的,但它在彩色条的中间,包括勾号在内,使它看起来太低,特别是当我有另一个图例在旁边的时候。

    library("factoextra")
    library("FactoMineR")
    
    data("decathlon2")
    df <- decathlon2[1:23, 1:10]
    
    res.pca <- PCA(df,  graph = FALSE)
    
    p<-fviz_pca_var(res.pca, col.var="contrib", 
                 gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
                 repel = TRUE)+
      theme(legend.position='bottom')
    
    p$labels$colour<-'Contribution to variance'
    

    enter image description here

    我想把它养大,我试过用 +guides(colour=guide_legend(title.vjust = 0.5))

    p<-fviz_pca_var(res.pca, col.var="contrib", 
                 gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
                 repel = TRUE)+
      theme(legend.position='bottom')+
      guides(colour=guide_legend(title.vjust = 0.5))
    
    p$labels$colour<-'Contribution to variance'
    

    但这可以去掉色条来交换信件有人能帮忙解决这个问题吗仅供参考,情节是 ggplot 反对谢谢你

    enter image description here

    1 回复  |  直到 8 年前
        1
  •  3
  •   Marius    8 年前

    你需要使用 guide_colourbar() 而不是 guide_legend() 尝试修改时:

    p <- fviz_pca_var(res.pca, col.var = "contrib", 
                    gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
                    repel = TRUE)+
        # You can use labs() to set labels
        labs(colour = "Contribution to variance") +
        guides(colour = guide_colourbar(title.vjust = 0.9)) +
        theme(legend.position = 'bottom')
    print(p)
    
    推荐文章