Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot legend at top but below title?

Tags:

r

ggplot2

Is there a way to make ggplot place the legend on top but below the title?

As an example...

enter image description here

..produced with the following code:

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

Any suggestions would be greatly appreciated?

like image 727
MikeTP Avatar asked Apr 23 '12 16:04

MikeTP


1 Answers

Edit Ignore this. The issue is not longer a problem. But the code has been updated so that it no longer throws an error.

While waiting for the next version, you can fine tune within ggplot2. For instance:

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 = "") 
like image 119
Sandy Muspratt Avatar answered Sep 30 '22 03:09

Sandy Muspratt