使用opts()更改图例位置/方向无效

时间:2011-08-19 23:58:32

标签: r ggplot2

创建以下绘图会导致图例垂直放置在右侧,而不是水平放置在底部,如调用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

如何在正确的位置获取图例?

1 个答案:

答案 0 :(得分:21)

问题是在调用theme_bw()之后放置了opts(),并重置了一些默认值。只需在theme_bw()之前放置opts()

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

注意:由于版本0.9.2 opts已由theme replaced完成:

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

enter image description here