How do I align the title and legend in one line in ggplot2 2.2.0?

library(ggplot2)
library(dplyr)
library(tidyr)
dfr <- data.frame(x=factor(1:20),y1=runif(n=20)) %>%
mutate(y2=1-y1) %>%
gather(variable,value,-x)
ggplot(dfr,aes(x=x,y=value,fill=variable))+
geom_bar(stat="identity")+
labs(title="Badass title")+
theme(legend.position="top",
legend.justification="right")
Changing the lineheight and/or vjust as title property doesn't seem to do anything.
ggplot(dfr,aes(x=x,y=value,fill=variable))+
geom_bar(stat="identity")+
labs(title="Badass title")+
theme(legend.position="top",
legend.justification="right",
plot.title = element_text(lineheight=-5,vjust=0))
Hardly perfect, but something like this will work:
ggplot(dfr,aes(x=x,y=value,fill=variable))+
geom_bar(stat="identity")+
labs(title="Badass title")+
guides(fill = guide_legend(direction = "horizontal")) +
theme(legend.position=c(1, 1.05),
legend.justification="right")

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