R ggplot图例在顶部但在标题下方?

R ggplot图例在顶部但在标题下方?,r,ggplot2,R,Ggplot2,有没有办法让ggplot将图例放在标题的上方而不是下方 例如 …使用以下代码生成: carrots<-list(Yield=c(345,226,74,559,288,194), Field=c("A","B","C","D","E","F"), Breed=rep(c("Long","Short"),each=3)) carrots<-data.frame(carrots) ggplot(carrots,aes(y=

有没有办法让ggplot将图例放在标题的上方而不是下方

例如

…使用以下代码生成:

carrots<-list(Yield=c(345,226,74,559,288,194), 
              Field=c("A","B","C","D","E","F"), 
              Breed=rep(c("Long","Short"),each=3)) 
carrots<-data.frame(carrots) 

ggplot(carrots,aes(y=Yield,x=Field,fill=Breed)) + 
  geom_bar() + 
  opts(title="Title",
       legend.direction = "horizontal", 
       legend.position = "top") + 
         labs(fill="") 

胡萝卜编辑忽略此项。这个问题不再是个问题。
但是代码已经更新,因此不再抛出错误

在等待下一个版本时,您可以在ggplot2中进行微调。例如:

ggplot(carrots, aes(y = Yield, x = Field, fill = Breed)) + 
  geom_bar(stat = "identity") + 
  theme(
     plot.margin = unit(c(2, 1, 1, 1), "cm"), 
     plot.title = element_text(size = 30, face = "bold", colour = "blue", vjust = 7), 
     legend.direction = "horizontal",
     legend.position = c(0.1, 1.05)) + 
   ggtitle("Title") +
  labs(fill = "") 

这在下一个版本中已经修复。现在,如果您可以访问Adobe Illustrator,请将绘图保存为eps,例如
ggsave(“plot.eps”)
,然后将图例移动到AI中的顶部。您可以使用gridExtra包微调网格排列的元素,并保存不同的元素(不同对象中的绘图、图例、标题)。这似乎在ggplot2的开发版本中得到了修复:install.packages(“devtools”)library(devtools)dev_mode(on=T)install_github(“ggplot2”)#立即使用dev ggplot2#完成后执行:dev_mode(on=F)#你又回到了马厩ggplot2@EtienneLow-Décarie with grid I不会在ggplot2中使用标题,而是手动将其添加到绘图上方(例如,使用
grid.arrange(plot,main=“title”)
)几天前发布的新版本修复了您的原始问题。