编辑构面/条带与绘图之间的距离

问题描述 投票:3回答:1

例如,

library(ggplot2)
ggplot(mpg, aes(displ, cty)) + geom_point() + facet_grid(cols = vars(drv))

enter image description here如何更改条带和主图之间的距离? (例如,在条带和主图之间创建间隙。) 但我不需要改变条带尺寸(与此edit strip size ggplot2不同)。

r ggplot2 facet-wrap facet-grid
1个回答
3
投票

这个问题可以有多种解决方案。

geom_hline

一个hacky就是在情节的顶部添加一条线(可能是白色,但这取决于你的主题)。我们可以使用geom_hline(或geom_vline,如果你的facet成行)。这造成了一种距离的幻觉。

library(ggplot2)
ggplot(mpg, aes(displ, cty)) +
  geom_point() +
  facet_grid(cols = vars(drv)) +
  # Add white line on top (Inf) of the plot (ie, betweem plot and facet)
  geom_hline(yintercept = Inf, color = "white", size = 4) +
  labs(title = "geom_hline")

strip.background

另一个解决方案(由@atsyplenkov建议)是使用theme(strip.background = ...)。在那里你可以指定边框的颜色。然而,这并不完美,因为它会从所有方向切割边界(可能有一种方法可以改善这一点)。

ggplot(mpg, aes(displ, cty)) +
  geom_point() +
  facet_grid(cols = vars(drv)) +
  # Increase size of the border
  theme(strip.background = element_rect(color = "white", size = 3)) +
  labs(title = "strip.background")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.