I am trying to align the legend title to the colorbar in ggplot2. How can I make the title vertically aligned to the colorbar only (instead of the whole colorbar plus number)?
Up now I get this result, trying various theme, element_text and guides options, whatever I do the vertical position of the title does not change.

library(ggplot2)
library(grid)
theme_set(theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position="bottom",
        legend.text=element_text(size=14),
        legend.title=element_text(size=14,hjust=-0.4),
        legend.direction='horizontal',
        legend.box = "vertical",
        strip.background = element_rect(colour='black',fill = "white"),
        plot.margin=unit(0.05*c(1,1,1,1),'npc'),
        plot.title=element_text(size=18),
        axis.title=element_text(size=18,vjust=0.2),
        axis.text=element_text(size=14),
        strip.text=element_text(size=14)))
ggplot(diamonds,aes(x,y,color=z))+
  geom_point()+
  scale_colour_gradient2('Time [min]',
                         low='lightgray',
                         mid='red3',
                         high='red4',
                         midpoint=15)
You can change vertical possition of legend title with the argument title.vjust= inside guide_colourbar= of function guides().
 + guides(color=guide_colourbar(title.vjust=1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With