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

J.C*_*Con 4 plot r legend ggplot2

在下面的示例中,我使用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'
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想提出它,我已经尝试使用 +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'
Run Code Online (Sandbox Code Playgroud)

但这摆脱了颜色条以换取字母。任何人都可以帮助解决这个问题吗?仅供参考,情节是一个ggplot对象。谢谢

在此处输入图片说明

Mar*_*ius 5

您需要使用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)
Run Code Online (Sandbox Code Playgroud)