R ggplot2:一行中的标题和图例

rmf*_*rmf 3 r ggplot2

如何将标题和图例对齐在一行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")
Run Code Online (Sandbox Code Playgroud)

更改lineheight和/或vjust作为标题属性似乎没有做任何事情.

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))
Run Code Online (Sandbox Code Playgroud)

Axe*_*man 6

很难完美,但这样的事情会起作用:

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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述