ggplot2,顶部和边缘的传说 [英] ggplot2, legend on top and margin

查看:153
本文介绍了ggplot2,顶部和边缘的传说的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Consider the following:

library(ggplot2)
library(grid)
ggplot(diamonds, aes(clarity, fill=cut)) + 
  geom_bar() +   
  theme(
    plot.margin=unit(x=c(0,0,0,0),units="mm"),
    legend.position="top",
    plot.background=element_rect(fill="red")) +
  guides(fill=guide_legend(title.position="top"))

输出如下所示:

plot.margin = unit(x = c(0, 0,0,0),units =mm)图例上方有一个不合适的白色(红色)空间。有谁知道如何补救?

The output of that looks something like this: In the context of plot.margin=unit(x=c(0,0,0,0),units="mm") there's an unseemly amount of white (red) space above the legend. Does anyone know how to remedy that?

感谢您的任何提示。

诚挚的,Joh

推荐答案

像你说的,我看不出你的例子,但我猜测margin是传说本身。您可以通过添加

Like you said, I can't see it in your example, but I'm guessing the margin is of the legend itself. You can eliminate the margin around the legend itself by adding

theme(legend.margin=unit(-0.6,"cm")) # version 0.9.x

到您的ggplot数字代码。

to your ggplot figure code.

更新:从版本2.1.0开始,语法已更改,您应该使用

UPDATE: As of version 2.1.0, the syntax has changed such that you should use

theme(legend.margin=margin(t = 0, unit='cm'))

请注意,至少现在,旧的解决方案仍然有效。

Note that, at least for now, the old solution still works as well.

这篇关于ggplot2,顶部和边缘的传说的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆