简体   繁体   English

如何增加情节周围的填充?

[英]How to increase the padding around a plot?

I have a horizontal barchart, with too-tight padding: 我有一个水平条形图,其填充太紧:

data <- data.frame(month = factor(c("Nov", "Dec", "Jan", "Feb")),
                   count = c(1489, 788, 823, 1002))

g <- (ggplot2::ggplot(data, ggplot2::aes(x=month, y=count)) +
        ggplot2::geom_col() +
        ggplot2::scale_x_discrete(limits=rev(data$month)) +
        ggplot2::coord_flip()) +

g

带有太紧填充的水平条形图

I am happy with the spacing between the bars, but I want a lot more space all around the 4-bar stack. 我对条形图之间的间距感到满意,但我想在4条形图条堆栈周围留出更多空间。 In other words, I want more padding around the central 4-bar figure. 换句话说,我想在中央的4柱形图周围添加更多填充。 The area of the light-gray rectangle that serves as the background for the 4-bar stack should remain unchanged, but the size of the 4-bar stack within that rectangle should shrink, with the net effect of produing wider padding all around the 4-bar stack; 用作4格堆栈的背景的浅灰色矩形区域应保持不变,但该矩形内4格堆栈的大小应缩小,从而产生在4格周围产生更宽的填充的净效果。 -酒吧栈; in other words, more of the light-gray background will be visible around the 4-bar stack. 换句话说,在4条烟囱周围会看到更多的浅灰色背景。

Also, I am looking for ways to do this that are entirely independent of the actual values on the axes. 另外,我正在寻找完全独立于轴上实际值的方法。 The code should produce the same visual effect whether the x-range is 0-1500 or 0-1500000 or altogether non-numeric (ie categorical). 无论x范围是0-1500还是0-1500000或完全是非数字(即分类),代码都应产生相同的视觉效果。

This means that the extra padding must be specified either (a) as percentages for the figure's total width and height; 这意味着必须指定以下额外填充:(a)为图形总宽度和高度的百分比; or (b) fixed numbers of pixels; 或(b)固定像素数;或 or (c) fixed units of measurement (cm, inches, printer points, etc.). 或(c)固定的度量单位(厘米,英寸,打印点等)。

Lastly, ideally , I would like to be able to specify the padding for all four edges independently. 最后, 理想情况下 ,我希望能够为所有四个边缘分别指定填充。

How can I do this? 我怎样才能做到这一点?

IMPORTANT I don't want to increase the margins around the light gray background. 重要信息我不想增加浅灰色背景周围的边距 I want to increase the padding within the light gray background. 我想增加浅灰色背景填充 If this distinction is not clear, please see this . 如果不清楚该区别,请参阅

You can use the expand_scale function within the expand argument to scale_x_continuous -type functions. 您可以在expand_scale函数的expand参数中使用scale_x_continuous函数。 It's a bit wordy, but... 有点罗word,但是...

ggplot(data, aes(x=month,y=count)) +
    geom_bar(stat="identity") + 
    scale_x_discrete(limits=(data$month), expand=expand_scale(mult=c(0.5,0.5))) + 
    geom_text(aes(label=count), hjust=-0.3) +
    coord_flip() + scale_y_continuous(expand=expand_scale(mult=c(0.5,0.5)))

Play around with the two elements of the mult vector, which define the padding above and below the axis, so you can change each side of the plot independently (although it's not exactly transparent, and will take some fiddling). mult矢量的两个元素,它们定义了轴上方和下方的填充,因此您可以独立更改图的每一侧(尽管它不是完全透明的,并且会有些麻烦)。 See ?expand_scale for more info. 有关更多信息,请参见?expand_scale

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM