Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing legend position/direction using opts() has no effect

Tags:

r

ggplot2

Creating the following plot results in the legend being placed vertically, on the right, rather than on the bottom horizontally as indicated in the call to opts():

dat <- data.frame(x = runif(10), y = runif(10), 
                  grp = rep(letters[1:2],each = 5))

ggplot(data = dat, aes(x = x, y = y, colour = grp)) + 
  geom_point() + 
  opts(legend.position = "bottom", legend.direction = "horizontal") + 
  theme_bw()

enter image description here

How do I get the legend in the correct spot?

like image 860
joran Avatar asked Aug 19 '11 23:08

joran


1 Answers

The problem is that that theme_bw() is placed after the call to opts(), and resets some defaults. Just place theme_bw() before opts():

ggplot(data = dat, aes(x = x, y = y, colour = grp)) + 
  geom_point() + 
  theme_bw() +
  opts(legend.position = "bottom", legend.direction = "horizontal")

Note: Since version 0.9.2 opts has been replaced by theme:

theme(legend.position = "bottom", legend.direction = "horizontal")

enter image description here

like image 95
joran Avatar answered Nov 10 '22 04:11

joran