本文介绍了在ggplot2和facet_wrap中,如何删除所有边距和内边距,同时保留strip.text?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能主要是由于我误解了panel.margin = unit(...)theme()函数中的工作方式...但是我无法按自己想要的方式在facet_wrap中自定义边距.基本上,我想要一个facet_grid看起来像这样,在每个构面中都插入有构面文本(即strip.text),并且每个构面之间都没有缝隙:

This might primarily be a result of me misunderstanding how panel.margin = unit(...) works in the theme() function...but I'm unable to customize margins in facet_wrap the way that I'd like. Basically, I want a facet_grid that looks like this, with facet text (i.e. strip.text) inset in each facet and no spcaing between each facet:

(我留在粉红色的边框中以显示每个构面的尺寸)

(I've left in the pink borders to show the dimensions of each facet)

所以这是到目前为止的代码.

So here's the code so far.

要设置数据并绘制图形,请执行以下操作:

To set up the data and plot:

library(ggplot2)
library(grid)
p <- ggplot() +
  geom_bar(data = mtcars, aes(x = cyl, y = qsec), stat = 'identity') +
  facet_wrap( ~ carb, ncol = 3)

mytheme <- theme_minimal() + theme(
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.ticks = element_blank(),
  axis.title = element_blank(),
  panel.margin = unit(0, "lines"),
  panel.border = element_rect(colour = rgb(1.0, 0, 0, 0.5), fill=NA, size=1)
)

标准剧情

 p + mytheme
p + mytheme + theme(strip.text = element_blank())
p + mytheme +
     theme(strip.text = element_text(size = rel(3.0), vjust = -4.0))

重新包含strip.text(以及增加的相对大小)会增加两行之间的垂直边距.因此,在这一点上,我不知道如何缩小顶部和底部行之间的垂直间隙.

The re-inclusion of strip.text (and the increased relative size) increases the vertical margin between the two rows. So at this point, I don't know how to close the vertical gap between the top and bottom rows.

p + mytheme +
    theme(strip.text = element_text(size = rel(3.0), vjust = -4.0),
          panel.margin = unit(c(-2, -2), "lines"))

那我如何只定位两行之间的panel.margin?

So how do I target just the panel.margin between the two rows?

修改:其他信息.行之间的间隔似乎是strip.background:

Additional information. The space between the rows appears to be strip.background:

p + mytheme +
        theme(strip.text = element_text(size = rel(3.0), vjust = -4.0),
              panel.margin = unit(-1, "lines"),
              strip.background = element_rect(fill = rgb(0, 1.0, 0, 0.2)))

推荐答案

theme()的可能参数列表中,不仅有panel.margin(分面面板(单元)周围的边距",请参见 ?theme ),但是很方便地,您也可以一次使用panel.margin.xpanel.margin.y(分面面板周围的水平/垂直边距(单位;继承自panel.margin)").

Among the list of possible arguments to theme(), there is not only panel.margin ("margin around facet panels (unit)", see ?theme), but conveniently, you can also access one of the axes at a time, with panel.margin.x and panel.margin.y respectively ("horizontal/vertical margin around facet panels (unit; inherits from panel.margin)").

因此,虽然将边距减小到零以下感觉有点像黑客,但以下内容将可以完成工作(您可能需要稍微调整一下值-unit(-2, "lines")对我来说效果最好):

Therefore, while decreasing the margin below zero feels a bit like a hack, something like the following will do the job (you might have to adjust the value a little - unit(-2, "lines") worked best for me):

p + theme(strip.text = element_text(size = rel(3.0), vjust = -4.0),
          panel.margin.y = unit(-2, "lines"))

如果使用strip.text = element_blank(),则可能应该使用panel.margin.y = unit(-0.5, "lines").

If you use strip.text = element_blank(), then you should probably use panel.margin.y = unit(-0.5, "lines").

这篇关于在ggplot2和facet_wrap中,如何删除所有边距和内边距,同时保留strip.text?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:12