多个图例的标题与ggplot2中的position =“bottom”不对齐

nou*_*use 5 r ggplot2

考虑这个情节:

library(scales)
library(ggplot2) 
df.m <- data.frame(Var1=c("A","B"), Var2=c("B", "A"), Similarity=c(97.5,92.5),
                       Rank=c("In", "Out"))

p <- ggplot(df.m, aes(Var1, Var2, fill=Similarity, col=Rank)) +
  geom_tile() +
  theme_bw()+
  theme(legend.position="bottom")+
  xlab("") +
  ylab("") +
  scale_fill_gradient2(low = muted("red"),
                       mid = "white", high = muted("blue")) +
  scale_color_manual(values=c("black", "gold")) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

图例的标题不一致。当我添加指南时,会发生这种情况:

p +   guides(color=guide_legend(override.aes = list(fill="white")),
         fill=guide_legend(title.hjust=0.5))
Run Code Online (Sandbox Code Playgroud)

标题现在对齐了,但填充图例的连续性丢失了。 在此输入图像描述

我能做些什么?

bee*_*oot 5

你可以加legend.box.just = "center"

ggplot(df.m, aes(Var1, Var2, fill = Similarity, colour = Rank)) +
  geom_tile() +
  theme_bw() +
  theme(legend.position = "bottom", legend.box.just = "center")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述