5

Consider this plot:

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")) 

enter image description here

The titles of the legends do not align. When i add guides, this happens:

p +   guides(color=guide_legend(override.aes = list(fill="white")),
         fill=guide_legend(title.hjust=0.5))

The titles now align, but the continous-nature of the fill-legend is lost. enter image description here

What can i do?

Community
  • 1
  • 1
nouse
  • 2,889
  • 2
  • 22
  • 45

1 Answers1

4

You can add 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")

enter image description here

erc
  • 9,993
  • 9
  • 54
  • 85