我认为这里的问题有点明显 . 我希望将图例放置(锁定)在“绘图区域”的左上角 . 出于多种原因,使用c(0.1,0.13)等不是一种选择 .
有没有办法改变坐标的参考点,使它们相对于绘图区域?
mtcars$cyl <- factor(mtcars$cyl, labels=c("four","six","eight"))
ggplot(mtcars, aes(x=wt, y=mpg, colour=cyl)) + geom_point(aes(colour=cyl)) +
opts(legend.position = c(0, 1), title="Legend placement makes me sad")
干杯
4 回答
Update: opts has been deprecated. Please use theme instead, as described in this answer.
默认情况下,引导的放置基于绘图区域(即,由灰色填充的区域),但是对齐是居中的 . 所以你需要设置左上角的理由:
如果要将指南放在整个设备区域,可以调整gtable输出:
Update: opts has been deprecated. Please use theme instead, as described in this answer.
只是为了扩展kohske的答案,所以对于下一个偶然发现它的人来说更为全面 .
我一直在寻找类似的答案 . 但发现
opts
函数不再是ggplot2包的一部分 . 在寻找更多时间之后,我发现可以使用theme
来做与opts类似的事情 . 因此编辑此线程,以便最大限度地减少其他时间 .下面是 nzcoops 编写的类似代码 .
此代码将给出完全相似的图 .
要扩展上面的excellend答案,如果要在图例和框外部添加填充,请使用
legend.box.margin
:这适用于
ggplot2
的最新版本,在撰写本文时为v2.2.1 .