Ggplot2: The legend overlaps the plot area - is it possible to manually adjust the position of the legend?

Suppose I have a dataset of carrot yield from different fields and different breeds:

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)

I want to build a graph showing the yield for each field colored by rocks:

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

But the legend always slightly overlaps the plot area:

plot with a little overlap of legends http://users.utu.fi/susjoh/Rplot.png

I tried to manually adjust the position of the legend, being out of the chart area, for example

opts(legend.position=c(0.5,1.1)

but then the graph fields cut off the legend, and I'm not sure how I can customize them. Is there a finer solution to this problem?

+5
source share
1 answer

, , , , :

ggplot(carrots,aes(y=Yield,x=Field,fill=Breed)) +
 geom_bar() +
 opts(legend.direction = "horizontal",
    legend.position = "top",
        legend.background = theme_blank()) + # this does hack
 labs(fill="")
+8

All Articles