How to move or position a legend in ggplot2 How to move or position a legend in ggplot2 r r

How to move or position a legend in ggplot2


In versions > 0.9.3 (when opts was deprecated)

theme(legend.position = "bottom")

Older version:

Unfortunately it's a bug in ggplot2 which I really really hope to fix this summer.

Update:

The bug involving opts(legend.position = "left") has been fixed using the most current version of ggplot2. In addition, version 0.9.0 saw the introduction of guide_legend and guide_colorbar which allow much finer control over the appearance and positioning of items within the legend itself. For instance, the ability specify the number of rows and columns for the legend items.


Just a few updates to these answers while I'm here. As Hadley mentioned, you can move a legend to the bottom with theme(legend.position = "bottom")

Or manually move with it theme(legend.position = c(.2,.85))

If you want the legend to be horizontal, use theme(legend.position = c(.2,.85), legend.direction = "horizontal")


You can always place the legend manually - but since the label is still stacked/vertical, it kind of looks ugly. I really hope hadley finds time to fix this :-)

p <- qplot(VarX,VarY, data=myDat, shape=Descrip) + opts(legend.position=c(.5,0.9),plot.margin = unit(c(6,0,0,0), "lines"))