【问题标题】:Adjusting legend title position in ggplot object, R调整ggplot对象中的图例标题位置,R
【发布时间】:2018-07-23 23:56:48
【问题描述】:

在以下示例中,我使用factoextraFactoMineR 创建双标图。该图有一个颜色条,并且标题在技术上居中在中间,但它位于颜色条的中间并且包含刻度数,使它看起来太低了,特别是当我旁边有另一个图例时。

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'

我想提高它,我已经尝试使用+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 对象。谢谢

【问题讨论】:

    标签: r ggplot2 plot legend


    【解决方案1】:

    尝试修改时需要使用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)
    

    【讨论】:

    • 如此简单!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2017-03-11
    • 2023-04-05
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多