创建以下绘图会导致图例垂直放置在右侧,而不是水平放置在底部,如调用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()
如何在正确的位置获取图例?
答案 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")