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()
How do I get the l开发者_运维技巧egend in the correct spot?
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")
精彩评论