如何在 ggplot2 中移动或定位图例
我正在尝试创建一个 ggplot2 图,图例位于图下方。
ggplot2 书在第 112 页上说“图例的位置和对齐方式由主题设置 legend.position 控制,值可以是右、左、上、下、无(无图例)或数字位置”。
以下代码有效(因为“right”是默认值),并且它也适用于“none”作为图例位置,但“left”、“top”、“bottom”均会失败,并显示“Error in grid.Call” .graphics("L_setviewport", pvp, TRUE) :视口的非有限位置和/或大小"
library(ggplot2)
(myDat <- data.frame(cbind(VarX=10:1, VarY=runif(10)),
Descrip=sample(LETTERS[1:3], 10, replace=TRUE)))
qplot(VarX,VarY, data=myDat, shape=Descrip) +
opts(legend.position="right")
我做错了什么?重新定位传奇一定是非常常见的,所以我想就是我。
I'm trying to create a ggplot2 plot with the legend beneath the plot.
The ggplot2 book says on p 112 "The position and justification of legends are controlled by the theme setting legend.position, and the value can be right, left, top, bottom, none (no legend), or a numeric position".
The following code works (since "right" it is the default), and it also works with "none" as the legend position, but "left", "top", "bottom", all fail with "Error in grid.Call.graphics("L_setviewport", pvp, TRUE) : Non-finite location and/or size for viewport"
library(ggplot2)
(myDat <- data.frame(cbind(VarX=10:1, VarY=runif(10)),
Descrip=sample(LETTERS[1:3], 10, replace=TRUE)))
qplot(VarX,VarY, data=myDat, shape=Descrip) +
opts(legend.position="right")
What am I doing wrong? Re-positioning a legend must be incredibly common, so I figure it's me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在版本> 0.9.3(当
opts
被弃用时)旧版本:
不幸的是,这是 ggplot2 中的一个错误,我真的非常希望在今年夏天修复它。
更新:
涉及
opts(legend.position = "left")
的错误已使用最新版本的ggplot2修复。此外,0.9.0 版本引入了guide_legend
和guide_colorbar
,它们允许更好地控制图例本身内项目的外观和位置。例如,该功能可以指定图例项的行数和列数。In versions > 0.9.3 (when
opts
was deprecated)Older version:
Unfortunately it's a bug in ggplot2 which I really really hope to fix this summer.
Update:
The bug involving
opts(legend.position = "left")
has been fixed using the most current version of ggplot2. In addition, version 0.9.0 saw the introduction ofguide_legend
andguide_colorbar
which allow much finer control over the appearance and positioning of items within the legend itself. For instance, the ability specify the number of rows and columns for the legend items.正如 Hadley 提到的,您可以使用
theme(legend.position = "bottom")
将图例移动到底部,或者使用
theme(legend.position = c(.2,.85) 手动定位))
如果您希望图例是水平的,请使用
theme(legend.position = c(.2,.85), legend.direction = "horizontal")
guides (fill =guide_legend(nrow = 2))
获取两行(分割图例)As Hadley mentioned, you can move a legend to the bottom with
theme(legend.position = "bottom")
Or manually positioned using
theme(legend.position = c(.2,.85))
If you want the legend to be horizontal, use
theme(legend.position = c(.2,.85), legend.direction = "horizontal")
guides(fill = guide_legend(nrow = 2))
to get two rows (split the legend)在较新版本的 ggplot2 中,您可以使用 + theme(legend.position='bottom')。
请参阅 Cookbook for R - Legends 了解更多信息传说善良。
作为对评论的回应,如果在 ggplot 中间调用
theme_update()
则不会启动(如+ theme_update()
中那样,仅在随后的时间中调用。它还修改活动主题而不仅仅是特定的图,因此您可以这样做:结果如上,区别在于后续图也将默认为底部的图例。
In newer versions of
ggplot2
, you can use+ theme(legend.position='bottom')
.See Cookbook for R - Legends for more legends goodness.
In response to a comment,
theme_update()
doesn't kick in if invoked in the middle of a ggplot (as in+ theme_update()
, only subsequent times. It also modifies the active theme rather than just the specific plot. So you could do this:with results as above, with the difference being that subsequent plots will also default to legend on bottom.
您始终可以手动放置图例 - 但由于标签仍然堆叠/垂直,因此看起来有点难看。我真的希望哈德利能抽出时间来解决这个问题:-)
You can always place the legend manually - but since the label is still stacked/vertical, it kind of looks ugly. I really hope hadley finds time to fix this :-)